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.
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
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;
}
}
Offline
The simplest way to go about solving this problem is nothing more than a bitwise AND operation.
Simply define your value as for example:
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:
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:
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
Last edited by RoboTwo (2014-11-12 11:30:35)
Offline
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?
Offline
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
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.
Offline
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
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
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