# Making combinations using random number generators

조회 수: 10 (최근 30일)
Chris Dan 2020년 8월 28일
댓글: Rafael Hernandez-Walls 2020년 8월 30일
Hello,
I have this vector
x0 = [1;88;88;2;2;2;2;2;-88;2]
Right now I change the values in this vector manually making combinations, I am looking for a random number generator which will generate combinations of numbers on these 10 slots, like a combination of 10 numbers from 0 till infinity, numbers can be decimals or integers as well or a combination of both
x0 = [x1;x2;x3;x4;x5;x6;x7;x8;x9;x10]
Does anyone knows how it can be done?
I have to use this vector elsewhere in the code...
##### 댓글 수: 2없음 표시없음 숨기기
Rafael Hernandez-Walls 2020년 8월 28일
In general, you can generate N random numbers in the interval (a,b) with the formula:
r = a + (b-a).*rand(N,1)
Chris Dan 2020년 8월 28일
Hi, how can i make it run in a loop, so at each iteration I get a new combination

댓글을 달려면 로그인하십시오.

### 채택된 답변

Rafael Hernandez-Walls 2020년 8월 29일
N=input('give me number of random elements:');
a=input('lower limit of the numbers to generate:');
b=input('upper limit of the numbers to generate:');
M=input('How many arrangements do you want to generate ?:');
r=zeros(N,M);
for k=1:M
r(:,k)=a+(b-a)*rand(N,1);
end
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Rafael Hernandez-Walls 2020년 8월 30일
oops!!!!
N=input('give me number of random elements:');
a=input('lower limit of the numbers to generate:');
b=input('upper limit of the numbers to generate:');
M=input('How many arrangements do you want to generate ?:');
r=zeros(N,M);
r(:,1)=a+(b-a)*rand(N,1);
for k=2:M
a=randperm(N);
r(:,k)=r(a,1);
end

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Steven Lord 2020년 8월 28일
If you want to draw with replacement use randi to generate numbers between 1 and the numel of the array you want to sample. Use those as linear indices into the array.
If you want to draw without replacement use randperm to generate the linear indices.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

R2019a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by