I have a c++ program that uses a random generator to create a cube. To create the cube I have this:
void generateSquare() {
int num = 0;
srand(time(NULL));
for (int r = 0; r < MSQUARE; r++) {
for (int c = 0; c < MSQUARE; c++) {
do {
num = (rand() % (MSQUARE * MSQUARE)) + 1;
} while (!checkUnique(num));
cube[r][c] = num;
}
}
}
It only allows a number to be generated one. This function is then wrapped in a do..while loop so that if the square doesn't meet a specific condition a new square will be generated until the one that does meet the condition is generated.
The problem is that it continually is generating the same square over and over again. I thought that the srand(time(NULL)) would seed the rand() function so that it would generate different random numbers each time to create a new unique square every time it is called but it looks like that is not happening.
void checkSquare() {
do {
generateSquare();
} while (!perfect);
}
This is not the actual do...while loop but gives you an idea of how it's being called.
How do I make sure the seed is unique each time?
You should call srand only once, at the beginning of your program. The reason why your seed is always the same is because time returns a number of seconds. Since you're calling it in a loop it always has the same value (until a second goes by).
Also if you can use C++11 or later you should look at the pseudo-random number generation in the standard library
srand sets the seed of the rand function. If you set it to a specific number it will always return you the same sequence of numbers. You will use this if you want to test a program, otherwise you use rand() without srand().
http://www.cplusplus.com/reference/cstdlib/srand/
Related
This question already has answers here:
How often should I call srand() in a C++ application?
(4 answers)
What does 'seeding' mean?
(4 answers)
Closed 3 years ago.
I'm wondering why it's advantageous to seed srand at the beginning of the program, instead of where I use it.
I generate pseudo-random numbers when seeding srand at the beginning of my program but I get the numbers all the same when I seed srand inside the function I call to generate the numbers
#include <iostream>
#include <ctime>
using namespace std;
int rng()
{
const int SIZE = 10;
int rng[10];
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
rng[i] = rand() % 128 + 1;
return rng[i];
}
}
int main()
{
int array;
//srand(time(NULL)); If i put it here i get actual random numbers
cout << "Welcome to the program";
cout << "\nthis is your rng\n";
for (int i = 0; i < 10; i++)
{
array = rng();
cout << array << endl;
}
return 0;
}
When I run the program all of the numbers are the same, but when I delete the seeding from in the rng function and uncomment the srand in the main module the numbers are pseudo-random which is what I want. Im wondering why though. I've looked into it and heard that im seeding srand with a time and when I run that function the loop iterates so fast that all of the numbers are generated with the same seed value so they're all the same, but I'm wondering what's the difference from that and having srand(time(NULL)) in main because either way doesn't the function generate the numbers so fast they'll be at the same seed value anyway? It doesn't appear that way because of the different output but im curious, why?
time returns number of seconds since 1.1.1970 so calling it repeatedly during one second will indeed return same values. It doesn't matter exactly where you put srand as long as it's before all rand calls and it should only be called once per program as it's global and obviously resets the random sequence. So if you use it only where you need it, you risk that when some other part of the code will need it too and calls srand again, it will interfere with your rand calls. It's not necessary to call it at all but then the seed will always be the same. It's good for debugging to have an option to set the seed deterministicly.
That said, don't use it, just don't.
As you observed time is not a good seed generator and rand is not even good random number generator, certainly not for floats and x mod n. Use <random> library. It has std::random_device which can generate true random numbers = good seeds. Sadly it's not required to. std::mt19937 is go-to RNG which together with std::XX_YY_distributions should be more than enough for everything but the most extreme need for randomness. It's also thread-safe because you control access to the generator and how it's used.
Im trying to shuffle a deck of cards but random_shuffle produces the same result every time
void
Deck::shuffle() {
cout << "SHUFFLING CARDS!!!\n\n";
srand(time(0));
random_shuffle(cards.begin(), cards.end());
displayCards();
}
That's because you seed pseudo-random number generator to the same value each time:
srand(time(0));
The granularity of time are seconds, if I'm not mistaken. If you pause the execution for some time between calls to Deck::shuffle() you should see different results.
Remove that line from the function and call it once at the start of your program.
I think that the problem is because you are putting srand(...) inside of your function.
Try to move it outside (so that it will be executed only once)
You are reseeding the random number generator each time you call shuffle.
You should only seed the random number generator once per application (typically in your application initialization):
int main()
{
// other initialization
srand(time(NULL)); // seed the number generator
// ...
}
It is important to know that to be able to receive a "random" number you have to seed the generator. Also it should be seeded outside the function.
srand(time(NULL));
The use of the time function will help ensure that you will receive a random number.
time.h does need to be included for it to work. For more reference on srand, click here.
I'm not familiar with random_shuffle, but here's a function that does a perfect shuffle - in other words, each 52! permutations of the deck has to be equally likely.
This is from Gayle Laakmann's Cracking the Coding Interview (Question 20.2)
void Deck::shuffle() {
int temp, index;
for (int i = 0; i < cards.size(); i++){
index = (int) (rand() %(cards.size() - i)) + i;
temp = cards[i];
cards[i] = cards[index];
cards[index] = temp;
}
}
I'm porting a python script to C++, which calls a function over thousand times. I've never programmed in C++ so i'm not sure what's happening and couldn't figure out a better title to the question.
This is not my actual function, but it reproduces the same error
int maior_sequencia()
{
int random;
srand(time(NULL));
int maior_sequencia = rand() % 6+1;
printf("%i", maior_sequencia);
return 1;
}
int main()
{
for(int i = 1; i<=100; i++)
{
maior_sequencia();
}
return 1;
}
If the first result from maior_sequencia() is 3, then it will output 3333333...
If i run the program again, and the first result is for is 5, it will output 555555...
Why is this happening?
int maior_sequencia()
{
int random;
srand(time(NULL));
//^^This will reset random seed every time you call this function
int maior_sequencia = rand() % 6+1;
printf("%i", maior_sequencia);
return 1;
}
Since you reset the random seed every time you call the maior_sequenciafunction, therefore, it will give you same value.
Try to put
srand(time(NULL));
inside main before the for loop instead.
The problem is two fold. First your use of srand and your use of time. When you seed a random number generator using time(), time is the number of seconds from some date in 1973, I forget the exact date, but it's known as linux time. Since you seed every time, you're resetting the random number generator, this is a good practice, however, since you're seeding it with something returning a number in seconds, it is likely your program is taking less than a second to run. Either do as someone else suggest, and seed the random number generator only once, or seed in something with a finer granularity... ms is probably acceptable.
I understand, using srand(time(0)), helps in setting the random seed. However, the following code, stores the same set of numbers for two different lists.
Wondering, how do I generate the different set of numbers when the following function gets called more than once.
void storeRandomNos(std::list<int>& dataToStore)
{
int noofElements = 0;
srand(time(0));
noofElements = (rand() % 14 ) + 1;
while ( noofElements --)
{
dataToStore.push_back(rand() % 20 + 1 );
}
}
Here is the rest of the code.
void printList(const std::list<int>& dataElements, const char* msg);
void storeRandomNos(std::list<int>& dataToStore);
int main()
{
std::list<int> numberColl1;
std::list<int> numberColl2;
storeRandomNos(numberColl1);
storeRandomNos(numberColl2);
printList(numberColl1 , "List1");
printList(numberColl2 , "Second list");
}
void printList(const std::list<int>& dataElements, const char* msg)
{
std::cout << msg << std::endl;
std::list<int>::const_iterator curLoc = dataElements.begin();
for ( ; curLoc != dataElements.end() ; ++curLoc)
{
std::cout << *curLoc << ' ';
}
}
A pseudo-random generator such as rand(), is simply a mathematical function that takes an input -- the seed -- and does some processing on it. It returns the new value it has produced and also sets that as the new seed. The next time it will use that new seed value.
Because computers are deterministic, every time you call rand() with the same seed, it will produce the same output value. That is why it is pseudo random.
In your example, you used the same seed twice because time(0) returns the time in seconds and your two function calls took place within that same second (because computers are pretty fast).
As the other commenters have said, it's only necessary to seed to a fairly random value (i.e. the current time) once.
Initialize the RNG only once, when the main program starts. Not each time you step into your function. Otherwise, chances are the function is called twice within the same second, which may give you identical results for time(0).
do the srand(time(0)) once at the beginning of your program.
You need to use srand(time(0)) only once in every thread that calls rand() in your program to get pseudo random numbers.
I'm in need of a C++ (pseudo, i don't care) random number generator that can get me different numbers every time I call the function. This could be simply the way I seed it, maybe there's a better method, but every random generator I've got doesn't generate a new number every time it's called. I've got a need to get several random numbers per second on occasion, and any RNG i plug in tends to get the same number several times in a row.
Of course, I know why, because it's seeded by the second, so it only generates a new number every second, but I need to, somehow, get a new number on every call. Can anyone point me in the right direction?
Sounds like you do it like this:
int get_rand() {
srand(time(0));
return rand();
}
Which would explain why you get the same number within one second. But you have to do it like this:
int get_rand() {
return rand();
}
And call srand once at program startup.
You only need to seed the generator once with srand() when you start, after that just call the rand() function. If you seed the generator twice with the same seed, you'll get the same value back each time.
You should only seed the PRNG once.
Boost.Random has a variety of pretty good random number generators.
If you're generating a large number of random numbers, you could try an XORShift generator. For longs (8 bit):
// initial setup
unsigned long x = ... init from time etc ...
// each time we want a random number in 'x':
x ^= x << 21;
x ^= x >> 35;
x ^= x << 4;
This code generates a unique random number only once.
#include <ctime>
# include <iostream>
using namespace std;
int main()
{
int size=100;
int random_once[100];
srand(time(0));
for (int i=0;i<size;i++) // generate unique random number only once
{
random_once[i]=rand() % size;
for(int j=0;j<i;j++) if (random_once[j]==random_once[i]) i--;
}
for ( i=0;i<size;i++) cout<<" "<<random_once[i]<<"\t";
return 0;