someone for a nice rand funtion ?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
alonetrio
Posts: 34
Joined: Sun May 15, 2005 12:10 am
Contact:

someone for a nice rand funtion ?

Post by alonetrio »

hi !

this time i post a little request.

Anyone to help me to code a little function that return rand number ?
i have no clue how to code this kind of (simple ? ) thing.

i dream about a fuction like this :

unsigned long PrivRand(unsigned long Min, unsigned long Max)
{
}

this function could return unsigned long between Min and Max.

Any code ? idea ? help ?

Thanx

AloneTrio
Guest

Post by Guest »

Well, first off, you should be able to find any source for pseudo-random number generators off the net.

Start with http://en.wikipedia.org/wiki/Random_number_generator and follow any source/reference links or otherwise google would be a good start.

This isn't really a PSP specific issue. Surely the PSP has a hardware random number generator but it should not be necessary for most purposes.

As for returning a number in the range [x .. y] I believe standard practice is to take the result from the random generator (whether a real number in the range [0 .. 1.0] or an integer in the range [0 .. MAXINT-1]) and use a multiplier and/or modulus to compute your own range.

In fact, chances are that newlib has such a random generator, if one were to build newlib for PSP's gcc. I dunno about stdlibc++, but it probably does too. But both of those have their own issues and many dev'ers often prefer to go without either libs.

But again, it should be easy for you to just cut and paste a generator into your code.
cheriff
Regular
Posts: 258
Joined: Wed Jun 23, 2004 5:35 pm
Location: Sydney.au

Post by cheriff »

Well, on ps2 i use http://www.math.sci.hiroshima-u.ac.jp/~ ... t19937ar.c (a few greps for rand in ps2sdk seemed to only return stubs), and it works well. I stripped it out, save for the init_genrand and genrand_int32 functions. For your usage you could do something like:
rand = (genrand_int32()%(max-min)) + min;
Of course, like any random sequence, you need to start it with a new seed each time (else you'd always get the same sequence) I use the current time on PS2.. not sure if you can access anything similar on PSP...
Damn, I need a decent signature!
Guest

Post by Guest »

cheriff wrote:Of course, like any random sequence, you need to start it with a new seed each time (else you'd always get the same sequence) I use the current time on PS2.. not sure if you can access anything similar on PSP...
Great additional pointer on the link, and mentioning random seed.

One thing about changing the seed each time, preferably based on some not-quite-but-reasonably-pseudo-random method such as current-time, is that this is especially good for production usage such as in games.

However, for debugging purposes, it is often good to use the same seed each time in order to ensure consistant results for each execution. If you know what results you are supposed to be getting each time, it can be easier to find out the bug thats throwing it off. Something I was quite thankful for way back when I was debugging neural network code across multiple architectures. (Sun, Matlab, and PS2 :)
Vampire
Posts: 138
Joined: Tue Apr 12, 2005 8:16 am

Post by Vampire »

Ridge Racers has the MT19937_Library (libmt19937.prx)

sceMt19937_ECF5D379 -> 0xECF5D379,sceMt19937Init
sceMt19937_F40C98E6 -> 0xF40C98E6,sceMt19937UInt
Last edited by Vampire on Wed Jun 22, 2005 5:11 pm, edited 2 times in total.
cheriff
Regular
Posts: 258
Joined: Wed Jun 23, 2004 5:35 pm
Location: Sydney.au

Post by cheriff »

gorim wrote:However, for debugging purposes, it is often good to use the same seed each time in order to ensure consistant results for each execution.
True. I only used it to select the colour / shape of the next tetris block, that could be debugged comparint printf's to my tables...
usually:
#ifdef DEBUG
seed = 0;
#else
seed = myTimeFunc();
#endif
does the trick for me...
Damn, I need a decent signature!
Guest

Post by Guest »

Vampire wrote:Ridge Racers has the MT19937_Library (libmt19937.prx)

sceMt19937_ECF5D379 -> 0xECF5D379,sceMt19937Init
sceMt19937_F40C98E6 -> 0xF40C98E6,???
Vampire, it would require him to purchase Ridge Racers in order to legally use this library, and he may not distribute it. This assumes he has a means to obtain said library.

This is hardly a reasonable solution for portable / distributable code, until such time that a general method of tapping into the PSP's random generator/functions is known.

I also certainly hope there is no suggestion that he obtain this from a download site.
weak
Posts: 114
Joined: Thu Jan 13, 2005 8:31 pm
Location: Vienna, Austria

Post by weak »

Code: Select all

static unsigned Seed1;	
static unsigned Seed2;

void initSeeds(void)
{
   Seed1 = sceKernelLibcTime((void *) 0);
   Seed2 = sceKernelLibcTime((void *) 0);
}

unsigned myRand()
{
	Seed1 = (Seed1 + 46906481) ^ Seed2;
	Seed2 = Seed1 ^ &#40; &#40;&#40;Seed2<<3&#41; | &#40;Seed2 >> 29&#41;&#41; + 103995407&#41;;
	
	return Seed1 - Seed2;
&#125;
think grover used that in his terrain demo. just had to add the init part.
User avatar
alonetrio
Posts: 34
Joined: Sun May 15, 2005 12:10 am
Contact:

Post by alonetrio »

at begging i had a look a this pseudo random from terrain demo,
but couldn't understand how to mod it, to get number "from" "to" i want.

So i decided to write to this marvelous forum ;)
weak
Posts: 114
Joined: Thu Jan 13, 2005 8:31 pm
Location: Vienna, Austria

Post by weak »

;)

int foo = myRand()%10; // random from 0 to 9
Guest

Post by Guest »

Yes, % is the C/C++ operator for mod. Pay more attention in your programming class. ;)
User avatar
alonetrio
Posts: 34
Joined: Sun May 15, 2005 12:10 am
Contact:

Post by alonetrio »

thx to all, mystery about random function is no more one ;)

see ya in a soon coming lame source code with marvellous rand function included ;)

Alonetrio
Post Reply