Random numbers

Started by
15 comments, last by Tai-Pan 21 years, 9 months ago
Please I would like to know how can I code a little random number generator function in C, It should be little and capable of generating random numbers from 1 to 32. Is there any tutorial or something similar? Ive been trying but cant get one to work. Thanks "Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
Advertisement
*sigh* Use the friggin forum search...

#include <stdlib.h>
#include <time.h>
...
//call this once when your program starts.
srand((unsigned int)time(0));
...
int RandNum = int((rand()/(double)RAND_MAX)*31)+1;
...

That might work.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on June 23, 2002 2:26:28 PM]

[twitter]warrenm[/twitter]

quote:Original post by Tai-Pan
Please I would like to know how can I code a little random number generator function in C, It should be little and capable of generating random numbers from 1 to 32. Is there any tutorial or something similar? Ive been trying but cant get one to work.

Is this a learning project, or do you need a random number generator? There''s one in the Standard Library (rand), as well as several publicly available random numbers with higher periods and better distribution (Mersienne Twister comes to mind).

If it is a learning process, realize that it is difficult to generate true randomness. The RNGs referred to above are actually all pseudo-random number generators (the sources are generally available; Google it). Some suggest using something like a radio antenna set to static and sampling at oscillating intervals, but that is probably overkill for your needs.

  #include <mmsystem.h>int GetRandNumber(int low, int high){    int temp = 0;    srand((int)timeGetTime());    high = high - low;    temp = rand()%high;    temp += low;    return temp;}  

This should work, i.e.
GetRandNumber(1, 32) will give you a number between 1 and 32

quote:Original post by ZealousElixir
*sigh* Use the friggin forum search...

I wonder if you realize that he said code an RNG?
You can use the built in pseudo-random function of your compiler:

int nRandomNumber = (rand() & 0x1F) + 1;

(or is there a reason for not using rand()?)
quote:Original post by ZealousElixir
int RandNum = int((rand/(double)RAND_MAX)*31)+1;


Sorry, but this cast is C++ (won't work in C) and "rand" only gives the linker point for the function


// edit - nix the second bit - as you added the () ...



[edited by - lessbread on June 23, 2002 2:29:47 PM]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
quote:Original post by Oluseyi
I wonder if you realize that he said code an RNG?


Yes, as a matter of fact, I did. What''s your point?

http://www.gamedev.net/community/forums/topic.asp?topic_id=96406
http://www.gamedev.net/community/forums/topic.asp?topic_id=39324
http://www.gamedev.net/community/forums/topic.asp?topic_id=47804
http://www.gamedev.net/community/forums/topic.asp?topic_id=29875

Peace,
Ze.



//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

Thank you people..I just wanted to learn how to code a VERY little function capable of choosing random numbers from 1 to 31..integers of course..thanks for your help.

"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
Ive been reading something..but I dont understand what the "seed" is.

"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"

This topic is closed to new replies.

Advertisement