|
Chris, Paul, That being said, Paul's solution can still be screwed, depending on how the system is adjusting for math. One out of 32767 times it will return a value 1 greater than his range. For a range 1 to 10 (normal return): 32766 / 32767 = 0.999969481 * 10 = 9.99969481. Truncate returns 9. +1 = 10. Top range. But, that 32767'th time, 32767 / 32767 = 1 * 10 = 10. + 1 = 11. Ooops. The larger the range returned the less this will happen, but it is still a possibility. In my functions, I had always looked for this, if RangeReturn = TopRange + 1, recursively call the subroutine. Other options include just throwing that "extra" one into the toprange. if RangeReturn = TopRange + 1, RangeReturn = TopRange. With one returning a number from 0 to .9999999999 this isn't a problem. Regards, Jim Langston Me transmitte sursum, Caledoni! Chris Rehm wrote: > > Paul Jackson wrote: > > Paul, the solution you found is the most accurate one. You can, as been > pointed out, simply divide the random number generated by the range of > the number you want and take the remainder. But that is not exactly > accurate. Let's suppose you wish to generate a number between 1 and > 100. 327 times out of 328 it will return a number between 0 and 99, add > one and you have the random number you wanted to generate. But once out > of 328 tries it will generate a number between 32700 and 32768, not > 32799. So there is no chance you will generate a number between 69-100 > using this method. So your random generation is slightly skewed. > > The method you have found, using the ratio of the number to it's range > and applying that to the range of number that you want will give as a > distribution that is as random as the numbers provided by the generator. > > All that said, it might be that the generator isn't all that flat > either, and you might not care if you are all that perfect in your > distribution (I don't know your application). So you just need to decide > between accuracy and ease of coding. +--- | This is the RPG/400 Mailing List! | To submit a new message, send your mail to RPG400-L@midrange.com. | To subscribe to this list send email to RPG400-L-SUB@midrange.com. | To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com. | Questions should be directed to the list owner/operator: david@midrange.com +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.