Skip to main content
gamedev.net
🔒 Locked

Random Events

Started by valkyr 8 replies 300+ views
Original Post
valkyr
valkyr
How do I create random events? I assume you would use a random number and an if statement, like: create randumnumber; if(randnum == 1) { whatever; } if(randnum == 2) { whatever2; } etc, etc, etc... If so, how do I create randnum numbers?
Robbo
Robbo

( rand () % whatever ), where whatever is the maximum value - 1 you want to assign.

So

int x = ( rand () % 5 ) will give numbers between 0 and 4.
hammerstein_02
hammerstein_02
presuming C++


  
#include <time.h>
#include <stdlib.h> //I think rand is in here


void main()
{
srand(time(NULL)); //Seed the random number generator (should be called once)


int upper_limit = 1000; //If you want a random number from 0-1000


int number = rand()%upper_limit;
}


Very rough.. think that is about right.. you would have to check. And I have seen it mentioned that that isn''t quite good enough and doesn''t produce perfectly random numbers
valkyr
valkyr
Ok, new prob, now when ever i try to compile my program I get a compile error called "Permission denied". I double click it(I''m using Dev-Cpp) and I get an error message in a window saying "This is a linker error! It''s a generic problem in your source files!" What does this mean, and how do I fix it?
CoiN
CoiN
Lo,

To make sure you get more randomised numbers, you can use:

srand(time(NULL));

Remember to include time.h for the time() function.

To create a random number you use the rand() function, an example of how to do this is shown below:

int num;
int max = 100;
int min = 1;

num = ((rand() % (max - min)) + min);

This will place a random number between max and min, into num.

This is all assuming you are using C though.

Cya....

COiN
walkingcarcass
walkingcarcass
most random number generators are arithmatic using prime numbers, xors, bit rotates etc. the function acts on "seed" values one of which is the "random" number generated the last time, the others are often generated as waypoint results or as the high dword result of a multiplication.

a random made up example
A=a large prime, B=another large prime, G=?
let C=high dword of A*seed
let D=low dword of A*seed
let E=high dword of B*seed
let F=low dword of B*seed
let G=(C+F)xor(D+E)
return G xor seed

if you are experimenting with your own, i highly reccomend you make a program to test entropy and search for repeats

********


A Problem Worthy of Attack
Proves It''s Worth by Fighting Back
spraff.net: don't laugh, I'm still just starting...
valkyr
valkyr
Ok, there''s just one small(well, could be big. depends on program) problem: the number generated is always the same number. The only way to change it is to recompile the program. I want to "refresh" the number everytime the program is run; or, better yet, every time a function is executed. How do I do this?
guppy
guppy
quote:
Original post by valkyr
Ok, there''s just one small(well, could be big. depends on program) problem: the number generated is always the same number. The only way to change it is to recompile the program. I want to "refresh" the number everytime the program is run; or, better yet, every time a function is executed. How do I do this?


sounds like you forgot the line :

srand( time( NULL ) );

That line sets the random seed to the current time ( in ms ) wich should be pretty unique.

/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''''t use it...
/Please excuse my bad spelling - My native language is binary not english|Visit meTake my advice - I don''t use it...
Stoffel
Stoffel
..and most times, you should seed the number only once .

  
// Stoffel''s cheezy fool-proof rand class

#include <stdlib.h>
#include <time.h>

class Rand
{
public:
static int get ()
{
static Initializer theSeed;
return rand ();
}
private:
struct Initializer
{
Initializer () { srand (time (NULL) ); }
};
};

BTW, this is not as efficient as doing it yourself (there needs to be a check for static initialization in each call to get), but it''s at least fool-proof.

PS, if you wanted to change get to have a variable max number (rather than RAND_MAX), this would be the correct way to do it (for MSVC only):

  
static get (int max)
{
assert (max <= RAND_MAX);
static Initializer theSeed;
return max * rand () / (RAND_MAX + 1);
}

Note I didn''t use modulo. You should never use modulo with rand (). (See here for exhaustive discussion).


Think Liberally.
valkyr
valkyr
quote:
Original post by guppy
sounds like you forgot the line :

srand( time( NULL ) );



Still doesn''t work...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.