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!

Random Number Generator

Status
Not open for further replies.

cloudy

ISP
Jul 14, 1999
17
US
I don't program personally. I prefer the hardware side of the universe ;-) However, I have a friend trying to learn C++, and asked if I might help. He is desperate. Anyway, his uncle tried to help him, and this is the gist of his message.......<br>
<br>
Remember, you are talking about a computer. Given the same<br>
&gt;starting point, a computer will always generate the same sequence of<br>
&gt;numbers from a random number generator. Secondly, the random number<br>
&gt;generator does not start to evenly distribute the numbers until the<br>
&gt;random number generator has run at least 50 times. The best way to make<br>
&gt;the random number generator to be truly random is to run the generator<br>
&gt;several times before you use the generator for your end purpose. My<br>
&gt;favorite idea is to read the computer's clock, multiple the minutes<br>
&gt;times the seconds, add the hours, add fifty, and run the random number<br>
&gt;generator that many times before using the generator.<br>
&gt;<br>
<br>
My friend seems to want to find some c++ code that will do what is described above. It seems like it would only be a few lines of code, but again I am no programmer beyond just having had courses in college. Could anyone generate a few lines of code that will do the above?
 
Your friend's uncle is partially right.<br>
<br>
While it is true that "given the same starting point,<br>
a computer will always generate the same sequence of<br>
numbers from a random number generator", it is not true<br>
that "the random number generator does not start to <br>
evenly distribute the numbers until the random number<br>
generator has run at least 50 times".<br>
<br>
The following is from the man page of rand():<br>
<br>
NAME<br>
rand, srand, rand_r - simple random-number generator<br>
<br>
SYNOPSIS<br>
#include &lt;stdlib.h&gt;<br>
<br>
int rand(void);<br>
<br>
void srand(unsigned int seed);<br>
<br>
int rand_r(unsigned int *seed);<br>
<br>
DESCRIPTION<br>
rand() uses a multiplicative congruential random-number generator with period 2^32 that returns successive pseudo-random numbers in the range from 0 to (2^15)-1.<br>
<br>
The function srand() uses the argument seed as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to the function rand(). If the function srand() is then called with the same seed value, the sequence of pseudo-random numbers will be repeated. If the function rand() is called before any calls to srand() have been made, the same sequence will be generated as when srand() is first called with a seed value of 1.<br>
<br>
rand_r() has the same functionality as rand() except that a pointer to a seed seed must be supplied by the caller. The seed to be supplied is not the same seed as in srand().<br>
<br>
<br>
Here is the code fragment you would need:<br>
<br>
#include &lt;stdlib.h&gt;<br>
<br>
int<br>
myrand(boolean firstTime) <br>
{<br>
if (firstTime) {<br>
unsigned int seed = 100; <br>
return srand(seed);<br>
} else {<br>
return srand();<br>
}<br>
}<br>
<br>
You can get fancy and use some complicated function to <br>
set the value of seed, if you want.<br>
<br>
Hope this helps.<br>
<br>
-Mart311
 
Mart311....<br>
<br>
Thanx....I'll pass that info along. :)<br>
<br>
Cloudy
 
As correctly mentioned by Mart311, you need to fetch a random seed value for rand() function to behave like real random number generator. <br>
I 'am writting a code . <br>
<br>
#include &lt;time.h&gt;<br>
#include &lt;stdio.h&gt;<br>
<br>
int main() {<br>
srand((unsigned) time(NULL));<br>
printf(&quot;%d&quot;,rand());<br>
}<br>
<br>
Does this answer your question ?<br>
Thanx<br>
Siddhartha Singh<br>
<A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A>
 
My preference, when I leave the development phase, is to use the number of micorseconds since midnight (or free disk space, or othere dynamic system variable) as the seed for the random number generator.&nbsp;&nbsp;This isn't a good idea <b>during</b> development as one may wish to exactly repeat a certain execution sequence until one is certain that it is working correctly. (But in this case, be <b>certain</b> to run the program against several different random values, and to test explicitly the extreme data points --- which may not otherwise get executed during the test phase).<br>
 
May be this can help u..!!<br><br>iInicio means iBegins,<br>iFin means iEnds, this are the values range that<br>you would like to generate, like example 0 - 10 or 50 - 100<br><br>int iRrand(int iInicio, int iFin)<br>{<br>&nbsp;int iNum;<br>&nbsp;iNum=iFin-iInicio;<br>&nbsp;double dTemp = ((rand()+(32767/(iNum+1)))/32767.0*iNum;<br>&nbsp;return (int)dTemp+iInicio;<br>}<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top