Displaying text randomly

Hi,

I have not go any experience with random number generators etc so this may or may not be possible. Any ideas / other posts etc that you could point me to would be great.

I would like to randomise the text (such as a simple message) within a label whenever my program opened.

I would assume that I would have a selection of Strings^ for the randomiser to pick from but I have know idea where to start looking for ways to do this.

Advice anyone?
Last edited on
1. Create an array of strings.
2. Generate a random number in the range of the size of the array of messages.
3. Use the number to index into the array.

srand(int n) initialises the PRNG.
rand() gets the next number in the sequence.
rand() % array_size gets the next number in the sequence in the range [0 .. range).
Hi kbw,

Thanks for this. I haven't had much to do with arrays before but you have definitely given me something to work with. Thanks!
Hi there,

Once again, kbw, thanks for your suggestions. If anyone is interested, the following code works:

Includes...
1
2
#include <string>
#include <ctime> 


In your Form load event (i.e. double click Form1's background) or any other way you want your label to display the random text:
1
2
3
4
5
6
7
8
9
array<String^> ^ Colour = gcnew array<String^>(5);
Colour[0] = "Red";
Colour[1] = "Blue";
Colour[2] = "Yellow";
Colour[3] = "Green";
Colour[4] = "Purple";

srand(time(NULL));
label1->Text = Colour[rand()%5];


Thanks!
Topic archived. No new replies allowed.