Archived

This topic is now archived and is closed to further replies.

Mad One

rand( ) how to use it

Recommended Posts

Mad One    122
ok i have seen rand ( ) used in some of my books, but none of them explain how to use it. i have seen it written like this rand()%7 i tried using it the same way and the same result came out. also what can be used to set up timers ?

Share this post


Link to post
Share on other sites
Evil_Greven    410
rand() is used to generate a random number.

If, for instance, you want to generate a number that can be 0,1,2,3,4,5,6,7,8, or 9, you would simply do this:

number=rand()%10;

If you wanted 0 through 99, you would do this:

number=rand()%100;

If you wanted 1 through 10, you would do this:

number = 1 + (rand()%10);

It''s simply a matter of working with the modulus operand.

However, you must note this: you will always get the same result every time you run your program unless you include some way of setting another thing that is called srand(); that is, it seeds the random number generating function.

Normally, I do this to set it up:

srand(GetTickCount());
number = rand()%100;

And then, every time I ran the program I would have a different result series. Close to true randomizing numbers.


As far as timers go, why not check out MSDN on Microsoft.com? It explains it there.

-Greven

Share this post


Link to post
Share on other sites
Oluseyi    2103
The period for rand (it''s a pseudorandom number generator, so every so many numbers the sequence starts over), defined as RAND_MAX isn''t that high. Even worse, the low bits of rand exhibit less variation than the overall value, so using the modulo operation on the result of a call to rand yield pretty poor results. You can combat this by masking high values and shifting down (right), or by using a RNG with a better distribution (eg Mersienne Twister).

Share this post


Link to post
Share on other sites
Evil_Greven    410
Nevermind about searching MSDN.

If you want to create a timer you can do something like this.

#define YOUR_TIMER_1SECOND 1
#define YOUR_TIMER_5SECOND 5

SetTimer(hwnd, YOUR_TIMER_1SECOND, 1000, NULL);
SetTimer(hwnd, YOUR_TIMER_5SECOND, 5000, NULL);

hwnd being your hWnd that is in your WinMain().

Now in the Message Handler of your program (where it takes cases for WM_QUIT etc), you'd put something like this:

case WM_TIMER:
{
//figure out which timer ran
switch(wParam)
{
case YOUR_TIMER_1SECOND:
{
//your 1sec timer fired.
break;
}
case YOUR_TIMER_5SECOND:
{
//your 5sec timer fired.
break;
{
default:
break;
}
//always tell windows you handled the message
return(0);
}

Then when your program is exiting you want to get rid of the timers.

So, in your WM_DESTROY case in the Message Handler, you'll want:

KillTimer(hWnd, YOUR_TIMER_1SECOND);
KillTimer(hWnd, YOUR_TIMER_5SECOND);

-Greven

edit: SCARY!
Oluseyi Posted - 22 March 2004 10:51:06 PM

We posted the exact same second.. I'm afraid!




[edited by - Evil_Greven on March 22, 2004 10:53:08 PM]

Share this post


Link to post
Share on other sites
SiCrane    11839
RAND_MAX isn''t the period for rand(), it''s the dynamic range. That is to say the value returned by rand() can go from 0 to RAND_MAX. The period would be the number of calls to rand() after which rand() starts repeating the same sequence of numbers. There''s no guarantee in any of the standards of the period of rand(), and there''s no standard way to get at the period.

Share this post


Link to post
Share on other sites
Couldn''t you solve the period problem by re-seeding with the current time after every few seconds?

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..

Share this post


Link to post
Share on other sites
SiCrane    11839
Maybe. Depends on the resolution of the time value you use to reseed the generator, the algorithm the pseudo-random number generator uses, phase of the moon, etc.

On the downside, reseeding the random number generator like that usually decreases the determinism of the system, which make debugging more difficult by reducing the capability to reproduce error conditions.

In any case, period of the underlying PRNG isn''t usually a big issue. The main issue with most rand() implementations is the bit variance that Oluseyi was discussing.

Share this post


Link to post
Share on other sites