gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Random Numbers

#56143 - thegamefreak0134 - Thu Oct 06, 2005 5:56 pm

I need to generate a random number inbetween two values. Essentially, I need to pick a number (an integer) from 0 to 6. How do I do that in code?
_________________
What if the hokey-pokey really is what it's all about?

[url=http:/www.darknovagames.com/index.php?action=recruit&clanid=1]Support Zeta on DarkNova![/url]

#56144 - poslundc - Thu Oct 06, 2005 6:04 pm

Check the FAQ.

Dan.

#56703 - Ultima2876 - Mon Oct 10, 2005 6:09 pm

Code:
s32 random(signed int min, signed int max)
      {
         seed *= 69069;
         if(seed == 0) {seed++;}
         return min + ((((max - min + 1) * (seed >> 16))) >> 16);
      }


Set seed to something reasonably random if possible. Perhaps have a framecounter before the first button is pressed, then ((number of frames << 12) * 531985) is the seed.