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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

more info need 2ffat!

Status
Not open for further replies.

aggro

Programmer
Jun 9, 2000
4
0
0
GB
I need to know how to assign the random number to 'x' as it is generated, cheers mate!
 
&nbsp;&nbsp;&nbsp;&nbsp;It may depend on how you are getting the random number. For example, <i>rand</i>&nbsp;&nbsp;can be found in <i>stdlib.h</i>. To use it, you would do something like:<br><br>int x;<br>x = rand(); // a random number<br>x = rand(); // another random number<br>x = rand(); // another random number<br><br>&nbsp;&nbsp;&nbsp;&nbsp;To use <i>random</i>, you will need to include <i>stdlib.h</i> and <i>time.h</i> for <i>randomize</i>. You could then use it like:<br><br>randomize();<br>int x;<br>x = random(100); // returns random integer between 0 and 99<br>x = random(100); // returns another random integer between 0 and 99<br>x = random(100); // returns another random integer between 0 and 99<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Another common random number generator is <i>srand</i> (also in <i>stdlib.h</i>) which uses a &quot;seed.&quot;<br><br>int x;<br>srand(12348);<br>x = rand(); // a random number<br>x = rand(); // another random number<br>x = rand(); // another random number<br><br>&nbsp;&nbsp;&nbsp;&nbsp;In reality, all of these are pseudo-random generators. <i>srand</i> is very popular since you can give it the current time as a seed to make it appear more random.<br><br>#include &lt;time.h&gt;<br>.<br>.<br>.<br>time_t t;<br>srand((unsigned) time(&t));<br>x = rand(); // a random number<br>x = rand(); // another random number<br>x = rand(); // another random number<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Hope this helps.<br> <p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top