Using Randperm between two values
조회 수: 29 (최근 30일)
이전 댓글 표시
I'm creating a random lottery number generator that have to fill different requirements. The first requirement was as followed:
Generate and display a list of 7 numbers from the numbers 1 through 59.
To complete it I used this
n = randperm(59); numbers = n(1:7)
However the next requirement was to:
Generate and display a list of 8 numbers from the numbers 60 through 75.
I cannot find a way to use randperm between to values with one of them not being 1. I tried using randi, however it repeats numbers and one of the requirements is to not repeat any numbers.
Thanks for the assistance.
채택된 답변
Geoff Hayes
2017년 8월 16일
Danté - if randperm(n) returns a row array containing a random permutation from integers from one to n inclusive, then couldn't you try something like
randperm(16,8) + 59
where randperm(16,8) will return eight integers selected randomly from 1 through 16. We add 59 so that the integers now fall in the interval (60,75) which I think is what you want.
추가 답변 (4개)
John BG
2017년 8월 17일
Danté
Just use the output of randperm to index [60:75]
set_60_75=[60:1:75]
set_60_75(randperm(16,8))
no repetitions
set_60_75(randperm(16,8))
ans =
65 75 66 74 70 71 62 63
>> set_60_75(randperm(16,8))
ans =
75 73 72 70 68 60 63 65
>> set_60_75(randperm(16,8))
ans =
68 69 60 72 71 74 64 61
>> set_60_75(randperm(16,8))
ans =
64 73 61 62 65 75 71 70
>> set_60_75(randperm(16,8))
ans =
65 74 73 75 68 64 70 62
>> set_60_75(randperm(16,8))
ans =
70 75 69 68 60 72 66 62
댓글 수: 2
Walter Roberson
2018년 9월 11일
Depends what you mean by "many"
There are some lotteries which guarantee that the top prize will be awarded, which they achieve by only selecting from combinations that were sold.
In such a lottery, if everyone were to purchase the same combination of numbers (assuming the lottery allowed complete choice) then everyone would share equally in the prize.
According to https://en.wikipedia.org/wiki/National_Lottery_(United_Kingdom), in one year that one of the UK lotteries was run, the top prize was shared by 133 winners.
sanjay sutar
2022년 12월 31일
i have a set of numbers like a= [2,2.5,3,3.5,4,5] . I want to create a set of 100 numbers which is random using numbers of set (a) only. can i use randperm for it. please let me know any other option.
댓글 수: 3
Image Analyst
2022년 12월 31일
allowableValues = rand(1, 100); % Has 100 values here, but could be any number you want.
randomIndexes = randperm(numel(allowableValues))
% Generate random values. It will have however many elements as are in allowableValues.
randomValues = allowableValues(randomIndexes)
% OR
allowableValues = [2, 2.5, 3, 3.5, 4, 5] % Has less than 100 values.
randomIndexes = randi([1, numel(allowableValues)], 1, 100) % 100 or however many you want to generate.
randomValues = allowableValues(randomIndexes) % Obviously some values will be repeated.
sanjay sutar
2023년 1월 1일
@steven lord. Thank you very much sir.2nd option worked exactly how I expected.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!