You are not logged in.
Lost Password?


Register To Post



 Bottom   Previous Topic   Next Topic

#1
Random numbers
Posted on: 5/10 22:23
PVB Elite
Joined 2008/4/26
USA
933 Posts
CoderContributorTop10 PosterHOTY09 3rdLong Time User (11 Years) PVBCC 2010 EntryPVBCC 2013 Entry
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?

Attach file:


h modlibgccvb.h Size: 49.41 KB; Hits: 61
Top

#2
Re: Random numbers
Posted on: 5/12 2:40
Nintendoid!
Joined 2016/7/21
158 Posts
Long Time User (3 Years)
Weird. I'm noticing the same thing. Compiles, says it runs, but it's not actually returning random.
Top

#3
Re: Random numbers
Posted on: 6/10 12:53
PVB Elite
Joined 2008/4/26
USA
933 Posts
CoderContributorTop10 PosterHOTY09 3rdLong Time User (11 Years) PVBCC 2010 EntryPVBCC 2013 Entry
Anyone? I'd really like to finish this Virtual Boy casino thing. I came so close.
Top

#4
Re: Random numbers
Posted on: 6/13 3:42
Nintendoid!
Joined 2006/3/15
Ecuador
233 Posts
PVBCC 3rdCoderLong Time User (13 Years)
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;
}
Edited by jorgeche on 2019/6/13 3:53
Top

 Top   Previous Topic   Next Topic


Register To Post