Original Post

So I was looking through DogP’s modlibgccvb.h file and found, beginning at line 812, some code for possibly getting a random number. I tried and tried various things but could not get it to display a random number. Can I even use this to begin with, and if so, how do I return a random number between 1 and 52?

3 Replies

Weird. I’m noticing the same thing. Compiles, says it runs, but it’s not actually returning random.

Anyone? I’d really like to finish this Virtual Boy casino thing. I came so close.

You use those functions as follows:

int yourRandomNumber = randnum(randseed(), 51) + 1;
// randnum will return a value between 0 and 50 inclusively, ie: 0, 1,… , 50, hence the need to add 1

But I don’t like to use them since randseed performs poorly when used inside an intensive loop (with little benefit according to my experience, that is, the number of passes it does doesn’t increase that much the “randomness”); and randnum is not safe since it doesn’t check if the randnums parameter is non zero and, on top of that, there is no action taken for negative numbers since the result of the modulo operation is implementation-defined for them.

In place of those, you can use these (they are not mine, but Isaku Wada’s, 2002):

long randomSeed()
{
static u32 seed = 7; /* Seed value */

if(!seed)
{
seed = 7;
}

seed ^= seed << 13; seed ^= seed >> 17;
seed ^= seed << 5; return (seed); } int random(long seed, int randnums) { return seed & randnums ? (int)(abs(seed) % abs(randnums)) : 0; } These will output the same numbers everytime since there is no way to produce real random numbers within this universe that I'm aware of besides, maybe, the rate of atom decaying... Anyway, you can add some randomness to the output by including values that come "from outside the system", like the user input and the elapsed time or the number of clock ticks. For example: int random(long seed, int randnums) { // _accumulatedUserInput is a global variable where you store or accumulate the user input so far // _ticks is a global variable where you store the amount of clock ticks so far seed += _accumulatedUserInput + _ticks; return seed && randnums ? (int)(abs(seed) % abs(randnums)) : 0; }

  • This reply was modified 5 years, 5 months ago by jorgeche.

 

Write a reply

You must be logged in to reply to this topic.