Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2016-12-14 15:00:54

moriczgergo
New member
From: Hungary
Registered: 2016-12-14
Posts: 2

rand() generates the same number again and again

I generate a random map with a random number generator. I start srand on the title screen, then after that, I generate a ton of random numbers. Those random numbers are always the same!

Offline

 

#2 2016-12-14 19:22:54

RoboTwo
Member
From: Sweden
Registered: 2014-10-29
Posts: 19

Re: rand() generates the same number again and again

Could you share a code snippet?
Is it possible that you set the random seed in the same loop you generate the random numbers in?


Shake well before use

Offline

 

#3 2016-12-15 01:06:36

moriczgergo
New member
From: Hungary
Registered: 2016-12-14
Posts: 2

Re: rand() generates the same number again and again

I set srand(); on the very first line of main().

Offline

 

#4 2016-12-15 17:05:17

AntonioND
Member
Registered: 2014-06-17
Posts: 134
Website

Re: rand() generates the same number again and again

If you set it to the same value always, the numbers that will generate rand() will always be the same.

You need to add something like a loop to wait for the user to press and read from register DIV, for example. That's a sort of reasonable unpredictable value, so it can be used as random seed. But only if you wait for the user input!

Offline

 

#5 2017-01-17 13:01:38

chris
Member
From: USA
Registered: 2016-11-27
Posts: 142
Website

Re: rand() generates the same number again and again

I am also wondering about how to get a random number. I searched for DIV on the wiki, but nothing came up.


My avatar is from Nintendo Power. I miss those days, so I decided to bring them back.

Offline

 

#6 2017-01-17 16:59:46

ifilot
New member
From: Eindhoven, The Netherlands
Registered: 2017-01-16
Posts: 4

Re: rand() generates the same number again and again

I think it is pretty well explained in this blogpost:
http://imrannazar.com/GameBoy-Emulation … pt:-Timers

and partly in the Gameboy CPU manual (http://marc.rawer.de/Gameboy/Docs/GBCPUman.pdf) at page 38.

You have a timer on the gameboy that is incremented 16384 times per second (that's the DIV register you mention). If you extract a value from that register (0xFF04) at random intervals, you basically get a random number. In other words, despite that that counter is not random, your time intervals are and thus you get pseudorandom numbers.

Hope it helps. :-)

Offline

 

#7 2017-01-17 18:27:01

chris
Member
From: USA
Registered: 2016-11-27
Posts: 142
Website

Re: rand() generates the same number again and again

But how would I pull its number out? I'm using C.
I don't know what to call it? 0xFF04? DIV_REG? What?


My avatar is from Nintendo Power. I miss those days, so I decided to bring them back.

Offline

 

#8 2017-01-18 03:21:56

ifilot
New member
From: Eindhoven, The Netherlands
Registered: 2017-01-16
Posts: 4

Re: rand() generates the same number again and again

I am not really sure when using C; so far I have only programmed Gameboys using assembly, so it is a bit of guess. Sorry for that.

Here goes. In C, you can refer to a memory location using a pointer. The value of that pointer is then equal to the memory address. So to create a pointer to the DIV register, you type:

Code:

unsigned char *ptr_div_reg = 0xFF04;    // set pointer to memory address

To get the value residing at the memory address of the pointer, you use the dereference operator "*", like so:

Code:

unsigned char random_number = *(ptr_div_reg);    // get value at memory address

Finally, to get a random number within a certain range from that you could use a modulus. To get a random number between 0 and 5, inclusive (i.e. a dice roll), you could use:

Code:

unsigned char dice_roll = random_number % 6;

Offline

 

#9 2017-01-18 14:28:08

chris
Member
From: USA
Registered: 2016-11-27
Posts: 142
Website

Re: rand() generates the same number again and again

Thank you so much! It worked!


My avatar is from Nintendo Power. I miss those days, so I decided to bring them back.

Offline

 

#10 2017-01-18 18:24:52

AntonioND
Member
Registered: 2014-06-17
Posts: 134
Website

Re: rand() generates the same number again and again

If you read from that register twice without enough time in between, you will probably read the same value, so be careful when using it. If you use it once or twice per frame, in different part of the code, you'll be fine. If you want to use it a lot, you'll need other system.

Offline

 

#11 2017-01-19 09:57:48

ifilot
New member
From: Eindhoven, The Netherlands
Registered: 2017-01-16
Posts: 4

Re: rand() generates the same number again and again

Indeed, good point. The alternative is to use the DIV register as a seed for a pseudorandom number generator. The only thing is that I'm not sure how computationally expensive the various RNG algorithms are given the GB platform.

Offline

 

#12 2017-01-19 17:22:14

AntonioND
Member
Registered: 2014-06-17
Posts: 134
Website

Re: rand() generates the same number again and again

The ideal thing is to wait for user input or something, then read the value and use it as seed. For example, if your game has a title screen, you could read its value after the user closes the screen, or something like that. DIV starts always at the same value, you can't even use it as a seed if srand is called always at the same time in your boot sequence.

Offline

 

#13 2017-02-22 23:42:57

chris
Member
From: USA
Registered: 2016-11-27
Posts: 142
Website

Re: rand() generates the same number again and again

Having trouble again. Trying to get a random number between 10 and 30. Can't do it. I tried this:

Code:

poweruptimerlimit = randomthing % 30;
poweruptimerlimit=poweruptimerlimit+10

but it won't work. How would I do this?


My avatar is from Nintendo Power. I miss those days, so I decided to bring them back.

Offline

 

#14 2017-02-23 12:02:21

DonaldHays
Member
From: Seattle
Registered: 2016-08-01
Posts: 36
Website

Re: rand() generates the same number again and again

Well, `randomthing % 30` will return a number from 0 through 29, and then you're adding 10 to that, so that will give you a range of 10 through 39.

If you want 10 through 30 (including 30), you want: (randomthing % 21) + 10
If you want 10 until 30 (excluding 30), you want: (randomthing % 20) + 10

Last edited by DonaldHays (2017-02-23 12:03:58)

Offline

 

#15 2017-03-27 23:27:44

chris
Member
From: USA
Registered: 2016-11-27
Posts: 142
Website

Re: rand() generates the same number again and again

More trouble.
So (randomthing % 2)+1 should give me a number between 1 and 3, right? But it's always giving me 2 and never 1 nor 3. What's going on?


My avatar is from Nintendo Power. I miss those days, so I decided to bring them back.

Offline

 

#16 2017-03-28 04:07:33

Xephyr
Member
From: France
Registered: 2015-02-26
Posts: 59

Re: rand() generates the same number again and again

(randomthing % 2)+1 should give you either 1 or 2.

If you try to read randomthing only, what kind of value do you obtain? Only odd values?

Offline

 

#17 2017-03-28 18:46:04

chris
Member
From: USA
Registered: 2016-11-27
Posts: 142
Website

Re: rand() generates the same number again and again

So I changed it to (randomthing % 3)+1 and it works okay now.


My avatar is from Nintendo Power. I miss those days, so I decided to bring them back.

Offline

 

#18 2017-03-28 20:17:37

MrElephant
Member
Registered: 2014-01-29
Posts: 40

Re: rand() generates the same number again and again

This reminds me of an earlier post http://gbdev.gg8.se/forums/viewtopic.php?id=288 
I dont know what you did differently that caused it not to produce random negative numbers.

Last edited by MrElephant (2017-03-28 20:18:14)


Working in Gameboy BASIC.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson