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:
generate a vector of random number

Subject: generate a vector of random number

From: Wendy

Date: 25 Feb, 2011 17:36:06

Message: 1 of 3

Hi all,

I would like to generate a vector of random numbers. The size of the vector is 28by1. The values are between 1 and 99. I want each element to be unique. Following is the code that I am using...

I though about using "rand": round(rand(99)*99+1), but the elements were not unique. Then I thought to use "randperm" as following, but min and max numbers are always 4 and 99.
A = randperm(28)';
B = A/28.*(99);
C = B.*((max(B)-min(B))/(99-1))+1;
D(:,ii) = round(C);

I was wondering if there is better way of doing this. Any suggestion is appreciated.

Thanks,
Wendy

Subject: generate a vector of random number

From: Paulo Silva

Date: 25 Feb, 2011 17:51:05

Message: 2 of 3

"Wendy " <wlq121@gmail.com> wrote in message <ik8pa5$8iq$1@fred.mathworks.com>...
> Hi all,
>
> I would like to generate a vector of random numbers. The size of the vector is 28by1. The values are between 1 and 99. I want each element to be unique. Following is the code that I am using...
>
> I though about using "rand": round(rand(99)*99+1), but the elements were not unique. Then I thought to use "randperm" as following, but min and max numbers are always 4 and 99.
> A = randperm(28)';
> B = A/28.*(99);
> C = B.*((max(B)-min(B))/(99-1))+1;
> D(:,ii) = round(C);
>
> I was wondering if there is better way of doing this. Any suggestion is appreciated.
>
> Thanks,
> Wendy

a=randperm(99)';
a(1:28);

Subject: generate a vector of random number

From: Wayne King

Date: 25 Feb, 2011 17:52:04

Message: 3 of 3

"Wendy " <wlq121@gmail.com> wrote in message <ik8pa5$8iq$1@fred.mathworks.com>...
> Hi all,
>
> I would like to generate a vector of random numbers. The size of the vector is 28by1. The values are between 1 and 99. I want each element to be unique. Following is the code that I am using...
>
> I though about using "rand": round(rand(99)*99+1), but the elements were not unique. Then I thought to use "randperm" as following, but min and max numbers are always 4 and 99.
> A = randperm(28)';
> B = A/28.*(99);
> C = B.*((max(B)-min(B))/(99-1))+1;
> D(:,ii) = round(C);
>

> I was wondering if there is better way of doing this. Any suggestion is appreciated.
>
> Thanks,
> Wendy

Hi Wendy, if you have the Statistics Toolbox, you can use randsample()

Y = randsample(99,28);

The above samples 28 values without replacement from the integers 1:99

Wayne

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