Subject: Re: Non-randomly distributed random numbers (a challenge) Date: Sun, 18 May 97 00:25:31 CEST From: dkw@wolferts.sub.de (Klaus Wolferts) Newsgroups: comp.sys.hp48 Bruce Horrocks writes: > > I want to produce a program that generates integer random numbers that > are not uniformly distributed, i.e. like loaded dice. > > Example: > > N % > - -- > 1 30 > 2 15 > 3 40 > 4 5 > 5 10 > > The generator should produce numbers from one to five but distributed > according to their percentage frequency, thus three would be returned > 40% of the time. > > I can do this by getting random numbers from 1 to 100 and then using an > IF statement to see whether the number falls between 1 and 30 to return > 1, between 31 and 45 to return 2 etc etc. > > I was wondering if there is a more efficient way of doing this that > avoids the IF statement (because I might well have more than just 5 > bands). Generate a list or an array of 20 elements, fill them with the values: 1...6: 1 7...9: 2 10..17: 3 18: 4 19..20: 5 Generate integer random numbers 1..20 and use them as an index to get the element pointed to by that index. Klaus -- dkw@wolferts.sub.de | Dr.-Ing. Klaus Wolferts Tel: +49 721 706016 | Ingenieurbüro für EDV + Elektronik Fax: +49 721 706017_|_Mitteltorstr. 45, D-76149 Karlsruhe (Nrt)