Jump to content
Sign in to follow this  
Ex-RoNiN

Help me get an a in c programming

Recommended Posts

Right, as we all know the standard rand() function in C will spit out random numbers in the region of 0 to n, where n is machine dependent. I think its 32 thousand and something for 32 bit machines.

Now, I need the random number generator so I can imitate some very primitive AI in my game that I have to write for my C class However, I need it to generate (randomly) either 0, 1 or 2.

This value will then be used in a switch statement etc. But the thing I need to know is: how do I tell the rand() function to find me random values from 0-2 Obviously just int's please

Oh, and does someone know some good programming message boards, I had a look on google but couldn't find anything decent

Cheers in advance mates smile.gif

Share this post


Link to post
Share on other sites

Gamedev.net

Pretty good message boards and articles covering aspects of AI, OpenGL, DirectX, Networking etc.

Also checkout Flipcode.com

Share this post


Link to post
Share on other sites

You are coding in C!!! I hope to god I never have to learn any of that in my life time. For a student rasied on JAVA, C++ is beastly enough!! I presume something like this this will work in C.

Taken from www.cplusplus.com:

/* rand example */

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main ()

{

/* initialize random generator */

srand ( time(NULL) );

/* generate some random numbers */

printf ("A number between 0 and RAND_MAX (%d): %d\n", RAND_MAX, rand());

printf ("A number between 0 and 99: %d\n", rand()%100);

printf ("A number between 20 and 29: %d\n", rand()%10+20);

return 0;

}

So basically you use the mod (%) operator (returns an int). rand()%3 should give you something between 0 - 2 inclusive.

srand ( time(NULL) ); seeds the number generator with the current system time and methinks it's general good practice. Dunno how this works in C though.

Tim!

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (TimNiceButDim @ April 14 2002,19:16)</td></tr><tr><td id="QUOTE">You are coding in C!!! I hope to god I never have to learn any of that in my life time. For a student rasied on JAVA, C++ is beastly enough!! I presume something like this this will work in C.

Taken from www.cplusplus.com:

/* rand example */

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main ()

{

 /* initialize random generator */

 srand ( time(NULL) );

 /* generate some random numbers */

 printf ("A number between 0 and RAND_MAX (%d): %d\n", RAND_MAX, rand());

 printf ("A number between 0 and 99: %d\n", rand()%100);

 printf ("A number between 20 and 29: %d\n", rand()%10+20);

 return 0;

}

So basically you use the mod (%) operator (returns an int). rand()%3 should give you something between 0 - 2 inclusive.

srand ( time(NULL) ); seeds the number generator with the current system time and methinks it's general good practice. Dunno how this works in C though.

Tim!<span id='postcolor'>

Ahhhhhh, now I know what was amiss!

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">#include <time.h><span id='postcolor'>

I didn't have that library included, and that caused the compiler to moan about

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">srand (time(0))<span id='postcolor'>. In particular, it moaned about time not being declared.

So I just put </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">srand (3) <span id='postcolor'>, which in combination with </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">rand () % 3<span id='postcolor'> would create a random sequence of numbers (if put in a loop), but not genuinely random numbers for every "step" in that sequence (ie u always get 20120012211).

Thank you very much smile.gif

Share this post


Link to post
Share on other sites
Guest

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Ex-RoNiN @ April 14 2002,18:07)</td></tr><tr><td id="QUOTE">I think its 32 thousand and something for 32 bit machines.<span id='postcolor'>

A bit is 0 or 1. 32 bit is therefore 2^32 = 4294967296

which is 4,3 billion (4,3 * 10^9).

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (denoir @ April 15 2002,01:08)</td></tr><tr><td id="QUOTE">wow.gif7--></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Ex-RoNiN @ April 14 2002,18wow.gif7)</td></tr><tr><td id="QUOTE">I think its 32 thousand and something for 32 bit machines.<span id='postcolor'>

A bit is 0 or 1. 32 bit is therefore 2^32 = 4294967296

which is 4,3 billion (4,3 * 10^9).<span id='postcolor'>

Never mind, it is not machine dependent anyway, it is compiler dependent.

Silly me tounge.gif

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×