forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

someone for a nice rand funtion ?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
alonetrio



Joined: 15 May 2005
Posts: 34

PostPosted: Tue May 24, 2005 3:08 pm    Post subject: someone for a nice rand funtion ? Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Tue May 24, 2005 3:37 pm    Post subject: Reply with quote

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.
Back to top
cheriff
Regular


Joined: 23 Jun 2004
Posts: 262
Location: Sydney.au

PostPosted: Tue May 24, 2005 3:41 pm    Post subject: Reply with quote

Well, on ps2 i use http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.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!
Back to top
View user's profile Send private message
Guest






PostPosted: Tue May 24, 2005 3:48 pm    Post subject: Reply with quote

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 :)
Back to top
Vampire



Joined: 12 Apr 2005
Posts: 138

PostPosted: Tue May 24, 2005 4:13 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
cheriff
Regular


Joined: 23 Jun 2004
Posts: 262
Location: Sydney.au

PostPosted: Tue May 24, 2005 4:25 pm    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message
Guest






PostPosted: Tue May 24, 2005 4:32 pm    Post subject: Reply with quote

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.
Back to top
weak



Joined: 13 Jan 2005
Posts: 114
Location: Vienna, Austria

PostPosted: Tue May 24, 2005 4:39 pm    Post subject: Reply with quote

Code:

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 ^ ( ((Seed2<<3) | (Seed2 >> 29)) + 103995407);
   
   return Seed1 - Seed2;
}


think grover used that in his terrain demo. just had to add the init part.
Back to top
View user's profile Send private message
alonetrio



Joined: 15 May 2005
Posts: 34

PostPosted: Tue May 24, 2005 5:18 pm    Post subject: Reply with quote

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 ;)
Back to top
View user's profile Send private message Visit poster's website
weak



Joined: 13 Jan 2005
Posts: 114
Location: Vienna, Austria

PostPosted: Tue May 24, 2005 5:21 pm    Post subject: Reply with quote

;)

int foo = myRand()%10; // random from 0 to 9
Back to top
View user's profile Send private message
Guest






PostPosted: Tue May 24, 2005 5:24 pm    Post subject: Reply with quote

Yes, % is the C/C++ operator for mod. Pay more attention in your programming class. ;)
Back to top
alonetrio



Joined: 15 May 2005
Posts: 34

PostPosted: Tue May 24, 2005 5:36 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group