Jump to content
  • Advertisement

Archived

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

Tai-Pan

Random numbers

This topic is 5839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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"

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

  
#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

Share this post


Link to post
Share on other sites
quote:
Original post by ZealousElixir
*sigh* Use the friggin forum search...

I wonder if you realize that he said code an RNG?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!