Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with random_shuffle() in <algorithm>

Status
Not open for further replies.

classT

Programmer
Oct 6, 2002
19
US
Hi all, does anybody know why random_shuffle() does not appear to be random at all. I mean, no matter how I try to "seed" random_shuffle() always returns the same results!

Thanks in advance!
 
This is because when the computer is unable to literally randomize. When the computer randomizes, it prints from a sequence of numbers. As a result, everytime the program is run, it selects from the same list so the same sequence is given.

When using the rand() % number(or variable); command, it does the same thing. To compensate for this problem, you could use srand(time(NULL)); to force the computer to extrapolate the random number from the sequence based on the time. And due to the fact that time constantly changes, it changes the number selected every second.

Unfortunately, I don't know the syntax you use to seed a random shuffle, but I believe its something along those lines.

Sorry I couldn't help you more.
 
I have been using srand((unsigned)time(NULL)) at the beginning of main() to seed it and I'm still getting the problem. Here's the code and output:
Code:
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>

using namespace std;

int main(void)
{
	srand((unsigned)time(NULL));
	
	char* s=&quot;ABCDEFGHIJ&quot;;
	cout << s << endl;
	for (int i=0;i<4;i++)
	{
		random_shuffle(s,s+strlen(s));
		cout << s << endl;
	}
	
	return 0;
}

// outputs...........

ABCDEFGHIJ
CIJDBEAHGF
CFBDEIGAHJ
IDJABEFGHC
DBJIFEGACH

Try it and see!
 
That's because srand has absolutely nothing to do with random_shuffle. random_shuffle produces the same string of pseudo-random results each time in its default version.

If you want to use the C random number generator with random_shuffle, you have to specify rand as the third argument to random_shuffle.

e.g.

Code:
srand(time(0));
random_shuffle(s, s + strlen(s), rand );

Alternately, use one of the random number generators at
 
thank you.... yes, I had finally figured it out but I can't understand why there would be a version of the function (the 2 parameter one) that is essentially &quot;not very random&quot; - weird!
 
Well, it's not that it's not very random (it's no less random than rand), it's just that there's not a way to seed it. It actually turns out to be a useful property when you're doing something like simulation, and you want the results to be random, but you also want them to be the same &quot;version&quot; of random every time.
 
e.g. Windows freecell game, where presumeably the &quot;game number&quot; is nothing more than a random number seed used to deal the cards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top