Reputation: 45
I am trying to use the rand() and srand() function to generate a random number in the index of an array, but it outputs the same thing every single time. What gives? I want my output to be able to display a different color every time I execute my code.
AREAS OF FOCUS:
void randomPick()
int random = rand() % 7; cout << "Random color = " << colors[random] << endl;
srand((unsigned int)time(NULL))
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class randColor
{
private:
string colors[7];
public:
// set element to assign values to array
void setElement(int index, string color)
{
colors[index] = color;
}
void printColor()
{
int i = 0;
for(i = 0; i < 7; i++)
{
cout << colors[i] << endl;
}
}
void randomPick()
{
int random = rand() % 7;
cout << "Random color = " << colors[random] << endl;
}
};
int main()
{
srand((unsigned int)time(NULL));
randColor RandomOne;
const string colors[7] = {"red", "orange", "yellow", "blue", "green", "indigo", "violet"};
for (int i = 0; i < 7; i++)
{
RandomOne.setElement(i, colors[i]);
}
RandomOne.printColor();
RandomOne.randomPick();
return 0;
}
Upvotes: 0
Views: 278
Reputation: 131395
am trying to use the rand() and srand() function to generate a random number in the index of an array, but it outputs the same thing every single time. What gives?
It doesn't output the same thing every time. Or rather - you're printing all the colors first - which is obviously not random - then you print the name of the randomly-chosen color. It's not the same thing every time...
That aside - you should avoid using rand()
(or watch the talk by Stephen Lavavej on this matter). Do it the C++ way instead!
Upvotes: 4
Reputation: 5192
As people have stated, you call the printColor()
function, which will always look the same. Only the last color should be randomized. And because you only call it once, it makes sense that you'll see repeats quite often since your data set is so small.
So I've made a few changes to your code. Namely, instead of the weaker srand()
/ rand()
/ modulo math
route, I use <random>
. Your class has a static PRNG attached to it, and now all the random number generation code is contained in your class. The last big change I made was to call randomPick()
multiple times so you can get a better view of the fact that the color is indeed changing on successive calls.
Depending on your intended use, it might be better to simply std::shuffle()
the array and then read all the elements.
I've also removed the line using namespace std;
and made the appropriate changes. It's a bad practice.
#include <iostream>
#include <random> // Better for C++
#include <string>
class randColor {
private:
std::string colors[7];
static std::mt19937 m_prng;
static std::uniform_int_distribution<int> m_dist;
public:
// set element to assign values to array
void setElement(int index, std::string color) { colors[index] = color; }
void printColor() {
int i = 0;
for (i = 0; i < 7; i++) {
std::cout << colors[i] << '\n';
}
}
void randomPick() {
int random = m_dist(m_prng);
std::cout << "Random color = " << colors[random] << '\n';
}
};
std::mt19937 randColor::m_prng(std::random_device{}());
std::uniform_int_distribution<int> randColor::m_dist(0, 6);
int main() {
srand((unsigned int)time(NULL));
randColor RandomOne;
const std::string colors[7] = {"red", "orange", "yellow", "blue",
"green", "indigo", "violet"};
for (int i = 0; i < 7; i++) {
RandomOne.setElement(i, colors[i]);
}
// RandomOne.printColor();
for (int i = 0; i < 10; ++i)
RandomOne.randomPick();
return 0;
}
}
Upvotes: 3