GP32 I Need Random Values


ConsoleTom

Member
Joined
Dec 4, 2003
Messages
106
Age
48
Location
Germany
Website
Visit site
Hi !

How can i get random values and control the range of them ? Let's say i want random values from 0 to 31 or 7 to 19. Code example ?

Should i use srand or rand !? How do i get almost random values that do not repeat each running of the program ?

Greetings

Tobias
 
you call srand once to set it up. it seems that if i dont do this then the random numbers are always the same everytime ran.

then you call rand.

#include <ctime>


srand(time(0));

whatever = 7 + (rand() % 12); // start at 7, 12 digits from 7 is 19


i beleive this is correct, right?
 
harminoff posted on Apr 28 2005 at 10:22 PM said:
you call srand once to set it up. it seems that if i dont do this then the random numbers are always the same everytime ran.

then you call rand.

#include <ctime>


srand(time(0));

whatever = 7 + (rand() % 12); // start at 7, 12 digits from 7 is 19


i beleive this is correct, right?

It's not all correct. If you use (rand() % 12) you will get values from 0 to 11 so 7 + (rand() % 12) will give you values from 7 to 18.

By the way, I'm not sure what does time(0) means (I suppose that it's something related with system clock, but as the GP32 doesn't have a real time clock I don't think that it would be useful), but I think the best method to get pseudoaleatory values in the GP32 is after hitting a button (for example the button that skip the intro screen) to use srand(GpTickCountGet()) in order to get a different seed each time.

Regards
 
Last edited by a moderator:
Hi,

I am having problems with this too using Mr.Mirkos SDK. It doesn't have support (it seems) for rand and srand. Comes up with errors to do with software vs hardware floating-point operations :(

There are actually random number generators you can download off the net, which come as a .c source file and header - but like all your answers already say, they are no good unless you seed them with a random number.

BTW - are the GP32s memory values always random at startup? Maybe you could just use something like this as a seed rather than a time:

Code:
unsigned long seed;
unsigned long *seedPointer;

seedPointer = 22492; // some 4-byte aligned mem address
seed =  *seedPointer;

Hopefully whatever is at mem location 22492 changes every startup. You may have to play with the address to make it work properly (i.e. Make sure it is not in the framebuffer or something). And probably also always ensure it is 4-bytes aligned (i.e. seedPointer/4 must always be 0 )
 
In Acidwarp and a couple of Mirko's example code, the random values come as a header file and the program just reads through them in order for "random" numbers. Kind of cheesy but it works.
 
pea posted on Apr 28 2005 at 11:44 PM said:
Hi,

I am having problems with this too using Mr.Mirkos SDK. It doesn't have support (it seems) for rand and srand. Comes up with errors to do with software vs hardware floating-point operations :(

There are actually random number generators you can download off the net, which come as a .c source file and header - but like all your answers already say, they are no good unless you seed them with a random number.

BTW - are the GP32s memory values always random at startup? Maybe you could just use something like this as a seed rather than a time:

Code:
unsigned long seed;
unsigned long *seedPointer;

seedPointer = 22492; // some 4-byte aligned mem address
seed =  *seedPointer;

Hopefully whatever is at mem location 22492 changes every startup. You may have to play with the address to make it work properly (i.e. Make sure it is not in the framebuffer or something). And probably also always ensure it is 4-bytes aligned (i.e. seedPointer/4 must always be 0 )

srand(gp_getRTC()); works fine here.

About those random number generators: the most famous is probably the mersenne twister. I haven't done any stats about the newlib one is as "good" as the standard mersenne twister, but I *do* know it's significantly smaller (checked that out when i was doing Liquid Seawolf, where I set myself a size limit of 32kb).
 
Last edited by a moderator:
I take back what I said. Sorry - but rand() does work with Mr.Mirkos SDK. My problem was that I couldn't find what RAND_MAX was... is it the standard integer range?

EDIT: re: random number generators
From what I can recall from uni, isn't a random number generator just a 16 (or 32 or whatever size you want) number. To get the next one, you AND/OR a couple of bits together and place it in another bit, and shift the whole lot by some amount?

Here are some links to different methods:
http://remus.rutgers.edu/~rhoads/Code/code.html
Look for Pseudo-random Number Generators in C about a quarter of the way down (about 15 methods!!)
 
Puck2099 posted on Apr 28 2005 at 11:02 PM said:
[...]
By the way, I'm not sure what does time(0) means (I suppose that it's something related with system clock, but as the GP32 doesn't have a real time clock I don't think that it would be useful), but I think the best method to get pseudoaleatory values in the GP32 is after hitting a button (for example the button that skip the intro screen) to use srand(GpTickCountGet()) in order to get a different seed each time.

Regards

GP32 does have a real time clock. Look here

By the way, I think the best moment to initialize the random seed with the tick count, is not when you start the program, as you will always get the same result.
The best moment to do it, is after the player has pushed at least one button, and the later the best. That way you can be sure to get a different tick count each time, as the user hasn't spend the same amount of time to push the button/s.

Oankali.
 
Last edited by a moderator:
Oankali posted on Apr 29 2005 at 08:09 AM said:
Puck2099 posted on Apr 28 2005 at 11:02 PM said:
[...]
By the way, I'm not sure what does time(0) means (I suppose that it's something related with system clock, but as the GP32 doesn't have a real time clock I don't think that it would be useful), but I think the best method to get pseudoaleatory values in the GP32 is after hitting a button (for example the button that skip the intro screen) to use srand(GpTickCountGet()) in order to get a different seed each time.

Regards
The best moment to do it, is after the player has pushed at least one button, and the later the best. That way you can be sure to get a different tick count each time, as the user hasn't spend the same amount of time to push the button/s.

Oankali, that is what I said, doesn't it? :p

Regards
 
Last edited by a moderator:
As far as I know you can call srand() at the beginning of your code. You will be getting the same numbers only in GeePee32 as the generator counts with the time that passed between powering on the GP32 and calling srand() - and this is always the same in GeePee32, unlike the real hardware.
This at least works fine in my GPSweeper game...
 
WhiteFalcon posted on Apr 29 2005 at 01:01 PM said:
As far as I know you can call srand() at the beginning of your code. You will be getting the same numbers only in GeePee32 as the generator counts with the time that passed between powering on the GP32 and calling srand() - and this is always the same in GeePee32, unlike the real hardware.
This at least works fine in my GPSweeper game...

I don't know with srand() but with the official Gamepark SDK, if you seed with this instruction GpSrand(GpTickCountGet()) at the beginning of the code, you get always the same result, even in real hardware.

@Puck: pos si, little lapsus.
 
Last edited by a moderator:
Blah posted on Apr 28 2005 at 03:52 PM said:
In Acidwarp and a couple of Mirko's example code, the random values come as a header file and the program just reads through them in order for "random" numbers. Kind of cheesy but it works.
Sorry for resurrecting an old thread, but I just noticed this.

Acidwarp uses rand(); and my port actually uses the Mersenne Twister, not a table. The reason for its deterministic behaviour is that as it's noninteractive (with some hacked-in but optional interactivity) I had no reliable entropy source.

I was thinking of making a new version that stays on Noah's face until a button gets pressed, and then seeds the RNG from that. Any opinions on that?

-- Kal.
 
Last edited by a moderator:
Back
Top