You are not logged in.
Lost Password?


Register To Post



 Bottom   Previous Topic   Next Topic

#1
Random numbers
Posted on: 2009/12/31 13:12
PVB Elite
Joined 2008/12/28
Slovenia
628 Posts
Highscore Top ScoreHighscore Top ScoreCoderContributor10+ Game RatingsLong Time User (10 Years) App CoderPVBCC 2010 EntryPVBCC 2013 Entry
I need a function for generating random numbers between 0 and 1. I've tried searching the Web for such functions, but every one seems to be targeted for a specific platform or is too difficult to adapt, so does anyone have a working function they would like to share?
Top

#2
Re: Random numbers
Posted on: 2009/12/31 13:34
Administrator
Joined 2000/1/8
Germany
2515 Posts
Highscore Top10Highscore Top ScoreCoder#1 PosterHOTY09 1stLong Time User (15 Years) App Coder90+ Game Ratings
There's one by DogP in libgccvb, but I think (correct me if I'm wrong) it only works on hardware.

I chose an easy solution for BLOX2 and VUE Snake, which is a counter which is increased on every CPU cycle. When the counter reaches a certain value, set it to 0 and count up again. Using the modulo operation, you can then always derive a random number from that value. If you want a random number between 0 and 255 for example:


random 
prng_counter 256;


If you only need a 0 or 1, you can of course always switch around the counter:


if(prng_counterprng_counter 0; else prng_counter 1;
Top

#3
Re: Random numbers
Posted on: 2009/12/31 13:46
PVB Elite
Joined 2008/12/28
Slovenia
628 Posts
Highscore Top ScoreHighscore Top ScoreCoderContributor10+ Game RatingsLong Time User (10 Years) App CoderPVBCC 2010 EntryPVBCC 2013 Entry
Well, by "between 0 and 1" I mean "larger than 0 but smaller than 1". I have tried using a counter like you said, but then I could only use it once?
Top

#4
Re: Random numbers
Posted on: 2009/12/31 14:32
PVB Elite
Joined 2008/4/26
USA
933 Posts
CoderContributorTop10 PosterHOTY09 3rdLong Time User (11 Years) PVBCC 2010 EntryPVBCC 2013 Entry
I too am interested in getting random numbers. What is the modulo operation?
Top

#5
Re: Random numbers
Posted on: 2009/12/31 15:36
PVB Elite
Joined 2008/12/28
Slovenia
628 Posts
Highscore Top ScoreHighscore Top ScoreCoderContributor10+ Game RatingsLong Time User (10 Years) App CoderPVBCC 2010 EntryPVBCC 2013 Entry
It's an operator that returns the remainder of a division, and it's represented by the % sign in C and usually by MOD in BASIC. For example, 10 % 3 equals 1.
Top

#6
Re: Random numbers
Posted on: 2009/12/31 15:51
Administrator
Joined 2000/1/8
Germany
2515 Posts
Highscore Top10Highscore Top ScoreCoder#1 PosterHOTY09 1stLong Time User (15 Years) App Coder90+ Game Ratings
Quote:

HorvatM schrieb:
Well, by "between 0 and 1" I mean "larger than 0 but smaller than 1".


I see. Hmm, you could do it the same way. Since working with floats is slow, you could use fixed point math. For example, use numbers between 0 and 100, where 100 is 1, 95 is 0.95, 3 is 0.03 and so on.


Quote:

HorvatM schrieb:
I have tried using a counter like you said, but then I could only use it once?


You always let it run in the background, and only derive random numbers from it. So you have an always available, rapidly changing "seed" to get a random number. A very simplified example:


while(1//main loop
{
   
prng_counter++;
   
do_something();
   if(
user_moves()) random prng_counter 100;
}


("prng" stands for "pseudo random number generator".)


Quote:

VirtualChris schrieb:
I too am interested in getting random numbers. What is the modulo operation?


HorvatM just explained it nicely. Just a little addition: you use the modulo operator to get numbers in a certain range. % 100 for example always returns numbers between 0 and 99 (both included), because the remainder can not become greater than the divisor. It's a very useful function. :)
Top

#7
Re: Random numbers
Posted on: 2009/12/31 16:18
PVB Elite
Joined 2008/12/28
Slovenia
628 Posts
Highscore Top ScoreHighscore Top ScoreCoderContributor10+ Game RatingsLong Time User (10 Years) App CoderPVBCC 2010 EntryPVBCC 2013 Entry
Oh... I think I get it now. Thanks.
Top

#8
Re: Random numbers
Posted on: 2009/12/31 16:43
PVB Elite
Joined 2008/4/26
USA
933 Posts
CoderContributorTop10 PosterHOTY09 3rdLong Time User (11 Years) PVBCC 2010 EntryPVBCC 2013 Entry
I don't understand. To use these examples, do I need to put
int random=0, prng_counter=0;
at the beginning of my program, or what? I put this code in my code, with the int random=0, prng_counter=0; line in and I just get an opponent that goes left, down, up and right with no random movement whatsoever.



   
e=e+1;
     
prng_counter=prng_counter+1;
   
random prng_counter 256;
   if ((
random<64) && (opponentx<160) && (e<60)) opponentx=opponentx+1;
   if ((
random>63) && (random<128) && (opponentx>-80) && (e<60))  opponentx=opponentx-1;
   if ((
random>127) && (random<196) && (opponenty<11) && (e<60))  opponenty=opponenty+1;
   if ((
random>195) && (opponenty>-40) && (e<60))  opponenty=opponenty-1;
   
// if (prng_counter>60) prng_counter=0;
   
if (e>60e=0;
Edited by VirtualChris on 2009/12/31 17:14
Top

#9
Re: Random numbers
Posted on: 2010/1/2 8:13
Nintendoid!
Joined 2009/8/11
USA
112 Posts
Long Time User (10 Years)
VirtualChris,

This random number method will not work for your application. The only way this random number generation will work is when you rely on user input as well.

Another way to generate "random numbers" is with random number tables. Basically, you make a large array, and fill it with "random" numbers. You could generate the numbers yourself by rolling dice. Here is a quick example...


static int rand_num_table[12] = {57237257212173213112121178727830271821097999};
int randomPointer 0;

int getRandom(int range) {
  
randomPointer++;
  if (
randomPointer >= 12randomPointer 0;
  return 
rand_num_table[randomPointer]%range;
}


Now whenever you want a random number, you would just call getRandom() with the range (so if you wanted a number between 0 and 9, it would be getRandom(10))

Now this would be pretty boring because each game would be the same. So one method is to pick a new starting point by using the title screen, like with this code...


//display warning graphic
while (vbReadPad()==0) {
  
randomPointer++;
}
  
randomPointer %= 12;


You can get even more "random" by changing the randomPointer whenever a button is pressed. Basically, have a counter in your main loop, and any time it sees a button is pressed, add the counter to randomPointer, then clear the counter (and of course make sure randomPointer is in range).

You would want more than 12 random numbers in your table too, maybe use excel to generate a lot, or find random tables online. And you may only want to use u8 rather than int being you will probably not need a range greater than 255 and negative numbers are generally not used for random numbers. And of course you would need to change the number 12 in the above examples to the actual size of the array you use (use the sizeof feature if the compiler supports it, I didn't check)

So what you should have learned by now is random numbers do not exist, but you can get "random enough" by incorporating tables, timers, and user input. All the above is from the top of my head, so use it as more of an explanation rather than actual code.
Top

#10
Re: Random numbers
Posted on: 2010/1/2 13:39
PVB Elite
Joined 2008/4/26
USA
933 Posts
CoderContributorTop10 PosterHOTY09 3rdLong Time User (11 Years) PVBCC 2010 EntryPVBCC 2013 Entry
OK, I understand that there is no such thing as a random number. But using your example, say I want a number from one to four. What would be the name of the variable that's value I made is from one to four?

static int rand_num_table[4] = {1234}; 
    
int randomPointer 0

    
int getRandom(int range) { 
    
randomPointer++; 
    if (
randomPointer >= 4randomPointer 0
    return 
rand_num_table[randomPointer]%range
    }

I have made it call a variable number by doing the following:

e
=e+1;
if (
e>60e=0getRandom(3);

By the way, your code did compile, so I'm using it.
Top

 Top   Previous Topic   Next Topic


Register To Post