0
Any doubt? Clear all your confusion about rand() & srand() in the answers of this question.
Why rand() is used? Why srand() is used? What are they? What are their outputs?
7 Réponses
+ 2
rand() & srand() functions are used to generate random numbers.
If we use rand() function for one time [i.e, cout<<rand();], it will produce a random number which is always 41 based on its algorithm. But if we use this function inside a for loop[i.e, for(x=0;x<=12;x++){cout<<rand()<<"\n";}], it will produce total 13 random numbers (as described by this code) including 41. Every time we use this code, we will gate same random numbers as output.
However if we want to generate random numbers between a desired range, we need to write the code as below-
cout<<(rand()%12);
This code will generate random numbers from 1 to 11. Now if we want to generate random numbers from 1 to 12, simply we just have to add 1 with the previous code as previous code is already able to generate random numbers from 1 to 11. The code will be-
cout<<1+(rand()%12);
Now as we know every time if we run rand() function, it will generate same random numbers. So if we want to generate different random numbers each & every time, we need to pass a seed value as argument in srand() function before rand() function. The code will be like-
srand(12);
for(x=0;x<=10;x++)
{
cout<<1+(rand()%12)<<"\n";
}
Here we will get 11 random numbers. But if we write srand(13) instead of srand(12), we will get 11 random numbers but they are different from previous 11 random numbers.
+ 4
@aklex - Ultimately to back you up, on seeking better sources...when "I just work here" means "Here Be Dragons".
A programmer wrote in [1], from Reference Source at [2]:
"Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position."
When I'd rather see [3]:
"Anyone contemplating the use of random numbers is strongly advised to read Knuth's analysis of the problems involved."
I'd want to know if float mantissa or an infamous "bit 0 just flips" issue is related, but a transcription bug seems to out-trump that [4]...
...because somebody apparently just copied into .NET's library, made a typo... or worse: felt the numeric choice was arbitrary (don't roll your own crypto) and Microsoft couldn't immediately fix it without breaking customer code.
[1] http://referencesource.microsoft.com/#mscorlib/system/random.cs,66
[2] https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx )
[3] https://gopherproxy.meulie.net/gopher.floodgap.com/0/archive/walnut-creek-cd-simtel/SIMTEL/SIGM/VOLS100/VOL148/RNDKNUTH.LIB
[4] https://connect.microsoft.com/VisualStudio/feedback/details/634761/system-random-serious-bug
+ 3
@Arnab Bhaumik I know you're documenting, which is really valuable for learning.
Just note that modulo, RAND_MAX and ranged queries nearly always add mod bias to the standard problems with PRNGs.
+ 2
https://www.sololearn.com/Discuss/247809/?ref=app
This is little bit clearer
my question seem to missed some letters :/
+ 2
You shouldn't be using rand() and srand() anyways, specifically in production code. They are very old and are part of the C standard, not c++. The modern way is to use the <random> header, which offers a lot more features and modern implementations.
+ 1
rand() is used in order to generate a random number between 0 and RAND_MAX, which depends on the library and is at least 32767.
This function is used to "throw a dice".
By using the modulo operator % you can reduce the number of faces of your dice from RAND_MAX to a smaller number, e.g. the usual 6 by the expression rand()%6.
The problem with rand() is that it is not truly random.
Always the same succession of "random" numbers is produced.
Therefore it is called "pseudo-random".
In order to make it a bit more random, a "seed" can be used.
For setting the seed, the function srand() is used.
E.g. you can call srand(1) before a call of rand().
This will produce the same as just calling rand() alone.
Any other seed will produce different results.
Even with this improvement the function is still only pseudo-random!
If you need a true random number, e.g. in a crypto engine, there are other ways. Do NOT use rand() and srand() for that!
0
I think you have a small mistake in your explanation.
cout<<(rand()%12);
produces a random number from 0 to 11, not from 1 to 11.
Chosing 12 as argument for srand() could be confusing in your example, because it is exactly the same number you also use for the range of random numbers. Using completely unrelated numbers would be better.