Random strings?

Showing one string from many

Page 1 of 1

11 Replies - 2257 Views - Last Post: 17 September 2010 - 07:02 AM Rate Topic: -----

#1 Boyan   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Random strings?

Posted 16 September 2010 - 04:55 PM

Hello.

I know it's an easy task, but somehow I'm unable to figure it out.

So, I want to create a quote generator, which will show one string in a label when the "Random" button is clicked.

For example:

        private void button1_Click(object sender, EventArgs e)
        {
            string q1 = "\"I find your lack of faith disturbing\" - Darth Vader";
            string q2 = "\"Obi-Wan is here. The Force is with him\" - Darth Vader";

            labelTxt.Text = q1.ToString();
        }



My question is what do I need to randomize the strings (the strings will be declared as in the code)?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Random strings?

#2 skyhawk133   User is offline

  • Head DIC Head
  • member icon

Reputation: 1967
  • View blog
  • Posts: 20,425
  • Joined: 17-March 01

Re: Random strings?

Posted 16 September 2010 - 04:58 PM

Here's a thread (From the "related threads" at the bottom of this page) that has some code for displaying random items from an array. I would load all your quotes in to an array and then pull a random position in the array.

http://www.dreaminco...a-array-string/

This post has been edited by skyhawk133: 16 September 2010 - 04:59 PM
Reason for edit:: Pasted wrong URL

Was This Post Helpful? 2
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Random strings?

Posted 16 September 2010 - 06:01 PM

Here's the basic concept. You need an array or List to start with. It needs to be in a larger scope than just your event handler method, because you don't need to recreate it every time you click a button.

Make it a class level private field, and load it in the constructor. You could save the quotes to an app.config file, or to a database, or to a text file, or just hard code them for now. Add each of them to the list or array. The list is nice because it is dynamically sized (you don't have to know how many you have to start).

Also, create a Random object in the same scope. Initialize it in the constructor as well. Just a simple Random random = new Random(); will do.

Now, in your method, you can get the next random from 0 to your list's size. So if you have 10 quotes, you would do random.Next(0, listName.Count);. Then just use that random int to get the string at that index. Bang, done.

This post has been edited by insertAlias: 16 September 2010 - 06:01 PM

Was This Post Helpful? 3
  • +
  • -

#4 Boyan   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Random strings?

Posted 17 September 2010 - 02:01 AM

Thank you guys. I will hard code the quotes, I won't be using data base or .txt file for now. I even thought I could use some kind of a Hashtable to solve this, but I'm stuck again here. :)

EDIT: I did it! The Force is Strong. :)

        private void unleashBtn_Click(object sender, EventArgs e)
        {
            string [] strString = new string[10];

            strString[0] = "Test";
            strString[1] = "Test 2";
            strString[2] = "Test 3";
            strString[3] = "Test 4";
            strString[4] = "Test 5";
            strString[5] = "Test 6";
            strString[6] = "Test 7";
            strString[7] = "Test 8";
            strString[8] = "Test 9";
            strString[9] = "Test 10";

            Random RandString = new Random();

            labelView.Text = strString[RandString.Next(0, strString.Length - 1)]; 
        }

This post has been edited by Boyan: 17 September 2010 - 02:22 AM

Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Random strings?

Posted 17 September 2010 - 04:49 AM

actually, you would want this line..

labelView.Text = strString[RandString.Next(0, strString.Length - 1)];



to be this...

labelView.Text = strString[RandString.Next(0, strString.Length)];



The reason is because the Next method of the Random class will return a number between the minimum parameter and the maximum parameter. However, it will only choose "up-to" the maximum parameter, but not actually choose the maximum parameter. Therefore, in your code above, you will never choose the last element in the array. You need to put the maximum parameter as the size of the array so that the random class will be able to choose the last element in the array.
Was This Post Helpful? 2
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7506
  • View blog
  • Posts: 15,556
  • Joined: 16-October 07

Re: Random strings?

Posted 17 September 2010 - 04:52 AM

Excellent. To make changes in the future easier, you can also do this:
string [] strString = new string[] {
	"Test","Test 2","Test 3","Test 4","Test 5",
	"Test 6","Test 7","Test 8","Test 9","Test 10"
};


Was This Post Helpful? 3
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Random strings?

Posted 17 September 2010 - 06:13 AM

You also want to move almost all of that code out of your button click event. You don't need to create a new Random and a new array and 10 new strings ever time you click the button. Create them once. Make them class level, and fill the array and initialize the Random in the constructor. (Is that an echo?)
Was This Post Helpful? 1
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Random strings?

Posted 17 September 2010 - 06:34 AM

One other thing to note:
RandString.Next(0, strString.Length - 1)


With this, you'll never ever pick the last element in your array. Rand.Next is a little confusing. The lower bound is inclusive, but the upper bound is exclusive. So, if I say Rand.Next(0,5), the possible numbers are 0, 1, 2, 3, 4. So, if your array is of length 10, your statement will only result in #s 0 through 8 being chosen, leaving index 9 out.

Remove the - 1.
Was This Post Helpful? 1
  • +
  • -

#9 Boyan   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Random strings?

Posted 17 September 2010 - 06:46 AM

Thanks everyone. :)

Isn't better to use a .txt file when I have more than, let's say, 50 strings (in my example, quotes to be randomized)? Any help with that? Thank you.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7506
  • View blog
  • Posts: 15,556
  • Joined: 16-October 07

Re: Random strings?

Posted 17 September 2010 - 06:52 AM

Reading from a file, I'd use the File.OpenText method. There's code on that page to show you what ReadLine looks like.

For your program, I'd load the string into List<string>. Arrays are ideal when you have a fixed number of elements. However, if you don't know how many elements to expect, then a container class like List is the preferred choice.

This post has been edited by baavgai: 17 September 2010 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#11 Boyan   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: Random strings?

Posted 17 September 2010 - 06:54 AM

I worked with reading/writing .txt files before, but I'm not sure how to write the strings in it, and make the program reads a random from the file. :S Thanks.
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Random strings?

Posted 17 September 2010 - 07:02 AM

You wouldn't read randoms from the file. You'd read the file contents once into an array/list in memory and then get a random entry from that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1