Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Generator help ?

Status
Not open for further replies.

Ciralia

Programmer
Oct 22, 2002
51
0
0
US
I have looked in many places on the internet for solutions to my problem. I need to have a random number generator in MIPS Assembly that can generate numbers with either a given range, or from an array. I have found one generator at fivemouse.com, and it takes in a seed to produce the numbers. However, I could not figure out how to get the correct range out of that generator. If I wanted the numbers 1-10 to be randomly generated, how would I code this?

Also, if anyone has any better suggestions on how to get random numbers, please, do tell :).
 
Exactly as you would in a high-level language. Get the full number, divide by 10 and take the remainder (which is between 0 and 9) and add 1 if you want 1 to 10.
 
What do you mean by full number? So if I wanted 1-10, I would take 10/10, remainder 0 + 1 = 1?
 
No, I mean take whatever number the random number generator produces. The generators will all produce very big numbers; I just gave you the standard way to take a very large random number (it doesn't matter at all how large) and reduce it to a range that suits your application.

It's necessary to go this way because if you wrote a random number generator with a seed of only 1 to 10 it could have only 10 states, and it would cycle through the same 10 states for ever, which wouldn't look very random. The idea of proper generators is that they cycle through a very large number of states (the seed is the current state!) in a way that's hard to follow.
 
What he means is...say the random number generated is 3726534. Divide it by the high limit of your requirement (e.g 10). You will get a remainder of 4, add one to it so that your range can be entirely emcompassed. Giving you a random number of 5

The reason to add 1 is that the divide will return 0 if the random number were to be entirely divisible by the 'high limit number' ie 3726530 would return a remainer of 0 if divided by 10, which is out of range the range 1 - 10, in the same way there would be no way of get 10 returned so by adding one you always have a way of hitting the high limit of your range.

Hope this helps
 
Thanks kevin, nicely put!
Ciralia, you will also find random number discussions in things like "Numerical Recipes" and, if you're feeling very brave, Knuth and his like. I've never pushed it too far, not having progressed beyond the need for pacmen that don't look too boring! Serious Monte Carlo integrating people and crypto-characters need more.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top