Help displaying a random string from list

I've searched the web for some ways to make random strings be created, and the only thing I could find was to produce a random letter from a string "ABC..etc".

However, I cannot find an answer for my situation. I want to start with a list/array/vector of color strings, and have 6 of them be displayed in a random order. This is a snippet of the beginning of the code I currently have:
1
2
3
4
5
6
    string colors[] = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "White", "Gray", "Black", "Brown"};
    const int NUMBER_OF_COLORS = 6;
    for ( int color_index = 0; color_index < NUMBER_OF_COLORS; color_index++ )
    {
        cout << "  " << colors[color_index] << " Worm" << endl;
    }

Basically, what I want is this: Instead of having the list always start at Red, then Orange, etc, until Violet, I want it to display 6 random colors from the list.

Example output:
1
2
3
4
5
6
  Gray Worm
  Red Worm
  Green Worm
  Blue Worm
  Black Worm
  White Worm


I am using the C++11 random_device number generator, but if you want to use another random generator to show me, that's fine.

I also have a question or two about assigning number values that can change randomly to each string value produced, but I'll save that for another thread once I have a single objective for that.
Last edited on
1
2
3
4
5
6
7
8
for ( int color_index = 0; color_index < NUMBER_OF_COLORS; color_index++ )
    {
       // change color_index to a modulo of the random number generated
       // you have 10 options
       // seed = random_number % 10

        cout << "  " << colors[seed] << " Worm" << endl;
    }
Last edited on
Hi, thanks for the reply and your help, I'm not sure if I did this the exact way you said to, but here's my code now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <random>

using namespace std;


int main()
{
    srand(time(0));
    string colors[] = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "White", "Gray", "Black", "Brown"};
    
    const int NUMBER_OF_COLORS = 6;
    for ( int color_index = 0; color_index < NUMBER_OF_COLORS; color_index++ )
    {
        int random_color = rand() % 10;

        cout << "  " << colors[random_color] << " Worm" << endl;
    }
}

It seems to work fine, I think it is set up correctly to make each color have the same chance of being selected as one of the initial six colors, though I could use confirmation on that.

However, even if it is set up correctly, there's one thing I forgot to mention beforehand: I also don't want it to able to have the chance of repeating a color in the list. I don't want, for example:
1
2
3
4
5
6
  Gray Worm
  Green Worm
  Brown Worm
  Gray Worm // same as first
  Red Worm
  Violet Worm

I want there to always be six different colors to start with for my simulation, but I'm not sure how to put the condition in the for loop, I appreciate the help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <algorithm>
#include <iterator>

int main()
{
    std::srand(time(0));
    std::string colors[] = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "White", "Gray", "Black", "Brown"};

    std::random_shuffle( std::begin(colors), std::end(colors) ) ;

    const int NUMBER_OF_COLORS = 6 ;
    for ( int i = 0; i < NUMBER_OF_COLORS; ++i ) std::cout << "  " << colors[i] << " Worm" << '\n' ;
}
Wow, that algorithm is very convenient, thanks a lot for showing me it! I wouldn't never thought about checking <algorithm>. Before I saw your reply, I was trying to make a vector that would go along with the code to check if one value of the vector was equivalent to the other value 6 times. I also found http://www.cplusplus.com/forum/general/7784/ but this is much simpler, thanks again. I should go look at the other functions in that header.

EDIT: Not sure if anyone will even see this, but I read over http://www.cplusplus.com/reference/algorithm/random_shuffle/ and according to it, the range is [first,last) and not [first,last].

Since last isn't included, why does it still correctly shuffle the "Brown" string (which is the last string to choose from)?

EDIT 2: Is it related to how http://www.cplusplus.com/reference/iterator/end/ says it goes "pass-the-end" element?
Last edited on
Topic archived. No new replies allowed.