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 2014-11-10 14:41:11

Crona
Member
From: Wisconsin
Registered: 2014-09-23
Posts: 58
Website

Random Number Generators?

I am trying to make a randomly generated dungeon system for big project but the gbdk rand.h gives me way to big numbers. I need to generate a random number between 0-7 which I could do easily but I dont know the exact range of values rand() generates. Could anyone give me some info on this?

Offline

 

#2 2014-11-11 07:50:26

a cat
Member
Registered: 2014-07-27
Posts: 98

Re: Random Number Generators?

you could just keep generating random numbers untill its the one you want

unsigned short rand_num;

while(1){


rand(rand_num);//get random number (i dont know how you do it)

if(rand_num < 1000){//if less than 1000 then accept it
break;
}
else{
continue;
}

}


a cat in need of knowledge!!

Offline

 

#3 2014-11-11 14:25:58

Crona
Member
From: Wisconsin
Registered: 2014-09-23
Posts: 58
Website

Re: Random Number Generators?

The range is to big for that to work it would stall on that for a really long time.

Offline

 

#4 2014-11-11 23:00:49

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

Re: Random Number Generators?

The simplest way to go about solving this problem is nothing more than a bitwise AND operation.
Simply define your value as for example:

Code:

int r = rand() & 7;

This will take the value and remove everything above the first 3 bits, giving us a max value of 1 + 2 + 4 (7)

More In-depth:
In case you don't know what a bitwise AND does, here's a little explanation.

AND in logic terms is a operator which takes 2 or more entries and checks if all of these are [On/ True/ 1], returning [On/ True/ 1] if such is the case and [Off/ False/ 0] if not.

example:

Code:

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

What a bitwise AND does, is it takes all the bits that makes up the two values you feed into it and returns a value consisting of the AND result of each pair.

for example, let's say we calculate a bitwise AND for the values 150 and 7:

Code:

01101001 = 150
AND
11100000 = 7
||||||||
01100000 = 6

No matter what we feed into the first value and second value, the result won't exceed either (as any AND against a 0 will return 0)
And thus you can use that to limit the range of your randomly generated number smile

Last edited by RoboTwo (2014-11-12 11:30:35)


Shake well before use

Offline

 

#5 2014-11-12 10:57:41

Crona
Member
From: Wisconsin
Registered: 2014-09-23
Posts: 58
Website

Re: Random Number Generators?

Thank you so much! That method wont work for an even number though, will it?

Offline

 

#6 2014-11-12 11:19:03

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

Re: Random Number Generators?

It will return any number between 0 and the number you AND it with.
Care to explain what you mean by not work for even numbers? Do you want to round it to a even number or what?


Shake well before use

Offline

 

#7 2014-11-13 15:49:57

Crona
Member
From: Wisconsin
Registered: 2014-09-23
Posts: 58
Website

Re: Random Number Generators?

Just curious. I thought that you wouldn't be able to use an even number because then the first bit would be 0, and whatever you and with zero you get zero, so the least significant digit would always be zero, and so any numbers it would generate after anding with an even number would be even.

Offline

 

#8 2014-11-13 16:25:24

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

Re: Random Number Generators?

Yes, if you AND with an even number it'll return a even number and if you AND with a uneven number it will return either a uneven or even based on the second number you input.
But given that you wanted a number in the range 0-7 you should be perfectly fine with this approach, as 7 is a uneven number and can thusly return both even and uneven numbers if used in a AND operation.


Shake well before use

Offline

 

#9 2014-11-14 06:27:30

G.H.O.O.N.
New member
Registered: 2014-07-15
Posts: 3

Re: Random Number Generators?

The standard way I've always known for doing this in c is using the modulus operator.

rand() % 7 + 1;

rand() returns a random number between 0 and a large number.
% 7 gets the remainder after dividing by 7, which will be an integer from 0 to 6 inclusive.
+ 1 changes the range to 1 to 7 inclusive.

http://stackoverflow.com/questions/8013 … o-use-rand

I don't know how this varies using gbdk as I haven't played around with it myself yet.
I've been thinking about random dungeon generation myself so will be good to know if it works.

Last edited by G.H.O.O.N. (2014-11-14 06:28:11)

Offline

 

#10 2014-11-17 19:35:17

Tauwasser
Member
Registered: 2010-10-23
Posts: 160

Re: Random Number Generators?

Crona, you are correct. RoboTwo's approach only works for numbers that are a power of two: 0, 1, 2, 4, 8, 16 etc.

Basically, RoboTwo's approach is a special case of G.H.O.O.N.'s approach. For N = 2^x, rand() % (N - 1) == rand() & (N - 1). So if you need N different random numbers, you can AND by N-1. Hence, rand & 7 will generate 0 thru 7, i.e. 8 (=N) states.
For all other numbers, you will have to use the actual formula with the modulo.

cYa,

Tauwasser

Offline

 

#11 2014-12-31 14:51:10

Stinaris
New member
Registered: 2014-12-28
Posts: 9

Re: Random Number Generators?

Be careful when using pseudo random number generators as you may end up with a not so random number.
I generally start a global counter off the minute the software starts up and increment it on every iteration of the main loop or on a vertical blanking interrupt. I then use this as my seed value for the pseudo random number generator. That way get a different (0-255) seed each time it's required.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson