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.

Beginners > check my buggy code!

#42807 - Impatient - Sun May 15, 2005 3:42 am

Help, why can't I create random numbers within the boundries of the 240x160 display????

Code:


   unsigned char x;
   unsigned char y;

   while(1);
      {
        x = rand();
        y = rand();

            ptr_vram [x+ y * 240] = RGB16(255,255,0);
      }
}

#42809 - DiscoStew - Sun May 15, 2005 4:37 am

What is the range of the rand() function, and what format is it outputting? If I am assuming correctly, it outputs a float/double value between 0 and 1, and because x and y are unsigned char, the values will most likely be 0. I suggest you make your own random function. Maybe rely on a hardware timer, or even go simpler than that. Check the forums here on randomizing.
_________________
DS - It's all about DiscoStew

#42812 - tepples - Sun May 15, 2005 5:43 am

C's rand() returns an integer, roughly evenly distributed in the range [0, RAND_MAX]. Many implementations of the C library set RAND_MAX = 32767, but your code will be more robust if it does not rely on this behavior, as it's subject to change from version to version of the C library.

I agree with DiscoStew: try implementing your own pseudorandom number generator, as then you'll know exactly how it works. Start with this Wikipedia article to become familiar with the idea, and then search the forum for random or pseudorandom. A linear congruential generator should be good enough for simple uses in real-time video games, unlike Mersenne Twister which can sometimes pause for a moment to refill the state vector with a new set of numbers.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.


Last edited by tepples on Sun May 15, 2005 5:48 am; edited 1 time in total

#42813 - sajiimori - Sun May 15, 2005 5:47 am

DiscoStew: False. rand() returns int. I don't know what the standard requires, but most implementations of rand() I've used have 15 bits of range.

Impatient: Why would you expect rand() to automatically return values between 0 and 239 the first time you call it, and 0 to 159 the second time you call it?

A quick-and-dirty way to get a random number in the range 0-max is rand()%(max+1).

#42814 - Impatient - Sun May 15, 2005 6:25 am

I was trying to "plot" a Pixel randomly.

Like this:
Code:

   while(1);
   {
      X_Pos = rand() % 240;
      Y_Pos = rand() % 160;
      ptr_vram[Y_Pos * 240 + X_Pos] = RGB16(255,255,0);
   }


@sajiimori
Why "+1" in the rand()???[/quote]

#42817 - jma - Sun May 15, 2005 7:04 am

sajiimori wrote:
A quick-and-dirty way to get a random number in the range 0-max is rand()%(max+1).


Another method which will be more evenly distributed in your range would be:

Code:
#define rand_range(int min, int max) \
  ((min) + rand() * ((max) - (min)) / (RAND_MAX + 1))


Also note that this (and the % version) fall apart if max is > RAND_MAX.

Jeff M.
_________________
massung@gmail.com
http://www.retrobyte.org

#42818 - sajiimori - Sun May 15, 2005 7:05 am

Imagine counting up with a variable n, and printing n%4. What will the values look like?

012301230123...

The highest number is one less than the right-hand side of the % sign, so if you know the highest number you want, then add one to get what goes on the right-hand side.

#42819 - Impatient - Sun May 15, 2005 7:07 am

Thanks everyone ....
I still just have a black screen and no random pixels. some stupid silly error somewhere

#42915 - Impatient - Mon May 16, 2005 6:09 pm

got everything working by now