Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how can generate random integer numbers?

Subject: how can generate random integer numbers?

From: Emanuele

Date: 17 Nov, 2005 16:46:58

Message: 1 of 9

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

Subject: how can generate random integer numbers?

From: Nasser Abbasi

Date: 17 Nov, 2005 22:06:05

Message: 2 of 9

 

"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

Subject: how can generate random integer numbers?

From: Subhasri krishnan

Date: 17 Nov, 2005 14:23:23

Message: 3 of 9


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

Hi,
You can use rand(uniform distribution) or randn(normal distribution) to
make a series. Do something like rand * 10 or some number and round
off. you can do a rounding off like if between 9 and 10 then 9 or
something like that. you can do the tweaking with some good round
operators.

Subject: how can generate random integer numbers?

From: Ken Campbell

Date: 17 Nov, 2005 17:53:59

Message: 4 of 9

Subhasri krishnan wrote:
> emanuele wrote:
>> Hi all
>> how can generate random integer numbers?
>> Thanks in advantage Emanuele
>
> Hi,
> You can use rand(uniform distribution) or randn(normal
> distribution) to make a series. Do something like rand
> * 10 or some number and round off. you can do a rounding
> off like if between 9 and 10 then 9 or something like
> that. you can do the tweaking with some good round
> operators.

It's probably not very efficient but to get a random number between 1
and n you could use

temp=randperm(n);
random_integer=temp(1);

Ken

Subject: how can generate random integer numbers?

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 17 Nov, 2005 23:18:56

Message: 5 of 9

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

Subject: how can generate random integer numbers?

From: Sahan

Date: 19 Mar, 2013 18:40:15

Message: 6 of 9

emanuele <emanuelemignosa@yahoo.it> wrote in message <ef1bf39.-1@webx.raydaftYaTP>...
> Hi all
> how can generate random integer numbers?
> Thanks in advantage Emanuele


Well, you can use randint function

eg: to get uniform random integer vector of (1x10) with random number 1 to 1000

 test = randint(1,10,[1 1000])

Subject: how can generate random integer numbers?

From: Francesco Perrone

Date: 19 Mar, 2013 18:49:05

Message: 7 of 9

"Sahan " <sahanbull@gmail.com> wrote in message <kiabef$bgo$1@newscl01ah.mathworks.com>...
> emanuele <emanuelemignosa@yahoo.it> wrote in message <ef1bf39.-1@webx.raydaftYaTP>...
> > Hi all
> > how can generate random integer numbers?
> > Thanks in advantage Emanuele
>
>
> Well, you can use randint function
>
> eg: to get uniform random integer vector of (1x10) with random number 1 to 1000
>
> test = randint(1,10,[1 1000])

I would use randi function.

Subject: how can generate random integer numbers?

From: Steven_Lord

Date: 19 Mar, 2013 21:22:22

Message: 8 of 9



"Francesco Perrone" <francesco86perrone@yahoo.it> wrote in message
news:kiabv1$d1i$1@newscl01ah.mathworks.com...
> "Sahan " <sahanbull@gmail.com> wrote in message
> <kiabef$bgo$1@newscl01ah.mathworks.com>...
>> emanuele <emanuelemignosa@yahoo.it> wrote in message
>> <ef1bf39.-1@webx.raydaftYaTP>...
>> > Hi all
>> > how can generate random integer numbers?
>> > Thanks in advantage Emanuele
>>
>>
>> Well, you can use randint function eg: to get uniform random integer
>> vector of (1x10) with random number 1 to 1000
>>
>> test = randint(1,10,[1 1000])
>
> I would use randi function.

If the numbers need to be generated with replacement from the discrete
uniform distribution on integer values, use RANDI. If they need to be
generated without replacement from the discrete uniform distribution, use
RANDPERM. If they need to be generated with a nonuniform distribution,
things get more tricky.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: how can generate random integer numbers?

From: Peter Perkins

Date: 19 Mar, 2013 22:02:58

Message: 9 of 9

On 3/19/2013 5:22 PM, Steven_Lord wrote:
> If the numbers need to be generated with replacement from the discrete
> uniform distribution on integer values, use RANDI. If they need to be
> generated without replacement from the discrete uniform distribution,
> use RANDPERM. If they need to be generated with a nonuniform
> distribution, things get more tricky.

For non-uniform generation (or uniform, for that matter) with or without
replacement, you can use datasample if you have access to the Statistics
Toolbox:

http://www.mathworks.com/help/stats/datasample.html

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us