Re: [Dev-C++] How do you get random numbers in devc++?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel G. <sp...@ho...> - 2002-03-29 10:38:53
|
<html><div style='background-color:'><DIV>
<P>Well you could try something like this...</P>
<P>NOTE: These are in C so if anyone has the C++ version let me know!</P>
<P>int rnd(void) // Remember to declare elsewhere by typing int rnd(void);</P>
<P>{</P>
<P>int r;</P>
<P>r = rand();</P>
<P>return(r);<BR></P>
<P>}<BR></P>
<P> </P>
<P>Or you may like to "seed" the randomiser whereby you give it more numbers to base its calculations on and mess things up more (so it's "more" random).</P>
<P>void main() // Or int main()</P>
<P>{</P>
<P> int x;</P>
<P> seedrnd();</P>
<P> for(x=0;x<100;x++)</P>
<P> printf("%i\t", rnd());</P>
<P>}</P>
<P>int rnd(void)</P>
<P>{</P>
<P> int r;</P>
<P> r=rand();</P>
<P> return(r);</P>
<P>}</P>
<P>void seedrnd(void)</P>
<P>{</P>
<P> int seed;</P>
<P> char s[6];</P>
<P> printf("Enter a random number:");</P>
<P> seed=(unsigned)atoi(gets(s));</P>
<P> srand(seed);</P>
<P>}</P>
<P> </P>
<P>There are easier ways of doing it of course, but I'll leave that for others to inform you.</P>
<P> </P>
<P>Daniel</P>
<P> </P>
<P> </P></DIV>
<DIV></DIV>
<DIV></DIV>>From: "chromatonus 712" <CHR...@HO...>
<DIV></DIV>>To: dev...@li...
<DIV></DIV>>Subject: [Dev-C++] How do you get random numbers in devc++?
<DIV></DIV>>Date: Fri, 29 Mar 2002 02:28:50 +0000
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>_________________________________________________________________
<DIV></DIV>>Get your FREE download of MSN Explorer at
<DIV></DIV>>http://explorer.msn.com/intl.asp.
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>_______________________________________________
<DIV></DIV>>Dev-cpp-users mailing list
<DIV></DIV>>Dev...@li...
<DIV></DIV>>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
<DIV></DIV></div><br clear=all><hr>Chat with friends online, try MSN Messenger: <a href='http://g.msn.com/1HM305301/11'>Click Here</a><br></html>
|