Using Randperm between two values
33 次查看(过去 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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!