Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: how can generate random integer numbers?
Replies: 8   Last Post: Mar 19, 2013 6:02 PM

 Search Thread: Advanced Search

 Messages: [ Previous | Next ]
 Roger Stafford Posts: 5,929 Registered: 12/7/04
Re: how can generate random integer numbers?
Posted: Nov 17, 2005 6:18 PM
 Plain Text Reply

In article <h97ff.19781\$tV6.15313@newssvr27.news.prodigy.net>, "Nasser
Abbasi" <nma@12000.org> wrote:

> "emanuele" <emanuelemignosa@yahoo.it> wrote in message
> news:ef1bf39.-1@webx.raydaftYaTP...

> > Hi all
> > how can generate random integer numbers?
> > Thanks in advantage Emanuele
> >

> First you need to decide on the range of random numbers. Then use
> rand(1) to generate and floating random number between 0 and 1, then
> map the generated number to the range you want, and then chop of the
> fractional part.
>
> for example, if you want a random interger between 0 and 100, then
> something like could be a solution
>

> >> round(rand(1)*100)
>
> ans =
>
> 48
>
> if you want random intergers between 0 and 1000, then
>

> >> round(rand(1)*1000)
>
> etc..
>
> if you want a random number between values other than 0 and some
> positive upper limit, then you need to use a slightly more advanced
> maping than the above. But the idea is the same. You use the random
> number generated by rand(1) and map that to whatever you want.
>
> Nasser

-----------------------
Actually, in round(rand(1)*100), you wouldn't get a uniform distribution
for all the integers from 0 to 100. The two end values, 0 and 100, would
each have half the probability of occurring that other numbers in this
range would have. You should use:

floor(101*rand) or
ceil(101*rand-1)

to get a uniform distribution. Each number would then have a probability
of 1/101 of being chosen. Or in general, to get uniformly distributed
integers in the range [m,n] (including the endpoints) use:

floor((n-m+1)*rand+m) or
ceil((n-m+1)*rand+m-1)

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford

Date Subject Author
11/17/05 Emanuele
11/17/05 Nasser Abbasi
11/17/05 Roger Stafford
11/17/05 Subhasri krishnan
11/17/05 Ken Campbell
3/19/13 Francesco Perrone
3/19/13 Steven Lord
3/19/13 Peter Perkins

© The Math Forum at NCTM 1994-2018. All Rights Reserved.