Thread Subject: choosing a number at random

Subject: choosing a number at random

From: Monica

Date: 8 Nov, 2009 01:45:04

Message: 1 of 7

I need Matlab to choose a number between 1 and 10 at random, but I'm not sure how to go about doing this.

Subject: choosing a number at random

From: Nasser M. Abbasi

Date: 8 Nov, 2009 01:59:14

Message: 2 of 7


"Monica " <moh__krez@hotmail.com> wrote in message
news:hd57r0$8c2$1@fred.mathworks.com...
>I need Matlab to choose a number between 1 and 10 at random, but I'm not
>sure how to go about doing this.

If you want the random number to be drawn from uniform distribution, which I
assume that is what you want, then do

r = 10*rand


--Nasser

Subject: choosing a number at random

From: Nasser M. Abbasi

Date: 8 Nov, 2009 02:04:14

Message: 3 of 7


"Nasser M. Abbasi" <nma@12000.org> wrote in message
news:VBpJm.18763$Wd1.2960@newsfe15.iad...
>
> "Monica " <moh__krez@hotmail.com> wrote in message
> news:hd57r0$8c2$1@fred.mathworks.com...
>>I need Matlab to choose a number between 1 and 10 at random, but I'm not
>>sure how to go about doing this.
>
> If you want the random number to be drawn from uniform distribution, which
> I assume that is what you want, then do
>
> r = 10*rand
>
>

opps, you wanted it from 1 to 10, then do

r = 1+9*rand

(see help on rand)

--Nasser

Subject: choosing a number at random

From: ade77

Date: 8 Nov, 2009 02:52:03

Message: 4 of 7

"Monica " <moh__krez@hotmail.com> wrote in message <hd57r0$8c2$1@fred.mathworks.com>...
> I need Matlab to choose a number between 1 and 10 at random, but I'm not sure how to go about doing this.

you might also try randi(max), generates random number from 1:max.

in your case randi(10)

Subject: choosing a number at random

From: Peter Perkins

Date: 11 Nov, 2009 15:18:55

Message: 5 of 7

Nasser M. Abbasi wrote:

> opps, you wanted it from 1 to 10, then do
>
> r = 1+9*rand

Nasser, this is exactly the reason why we added RANDI a couple of releases ago: too easy to slip up.

Subject: choosing a number at random

From: Greg Heath

Date: 12 Nov, 2009 00:22:02

Message: 6 of 7

On Nov 11, 10:18 am, Peter Perkins
<Peter.Perk...@MathRemoveThisWorks.com> wrote:
> Nasser M. Abbasi wrote:
> > opps, you wanted it from 1 to 10, then do
>
> > r = 1+9*rand
>
> Nasser, this is exactly the reason why we added RANDI a couple of releases ago: too easy to slip up.

Does rand ever generate 0 or 1?

Greg

Subject: choosing a number at random

From: Nathan

Date: 12 Nov, 2009 00:30:18

Message: 7 of 7

On Nov 11, 4:22 pm, Greg Heath <he...@alumni.brown.edu> wrote:
> On Nov 11, 10:18 am, Peter Perkins
>
> <Peter.Perk...@MathRemoveThisWorks.com> wrote:
> > Nasser M. Abbasi wrote:
> > > opps, you wanted it from 1 to 10, then do
>
> > > r = 1+9*rand
>
> > Nasser, this is exactly the reason why we added RANDI a couple of releases ago: too easy to slip up.
>
> Does rand ever generate 0 or 1?
>
> Greg

From the documentation:
"r = rand(n) returns an n-by-n matrix containing pseudorandom values
drawn from the standard uniform distribution on the open interval
(0,1)."

Meaning, 0 < r < 1

-Nathan

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com