Using Randperm between two values

46 vues (au cours des 30 derniers jours)
Danté Davids
Danté Davids le 16 Août 2017
Commenté : sanjay sutar le 1 Jan 2023
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.
  1 commentaire
Paul Munro
Paul Munro le 3 Sep 2018
59+randperm(1,16)

Connectez-vous pour commenter.

Réponse acceptée

Geoff Hayes
Geoff Hayes le 16 Août 2017
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.
  1 commentaire
Danté Davids
Danté Davids le 17 Août 2017
Ah, it seems so simple now. This really helped me better my understanding of how Matlab works. Thank you.

Connectez-vous pour commenter.

Plus de réponses (4)

Star Strider
Star Strider le 16 Août 2017
Try this:
NextRequirement = 60 + randperm(15, 8)

John BG
John BG le 17 Août 2017
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 commentaires
John BG
John BG le 17 Août 2017
Modifié(e) : John BG le 17 Août 2017
ok, but lottery systems, by definition, cannot have many winners, can they?
Walter Roberson
Walter Roberson le 11 Sep 2018
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.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 16 Août 2017
Hint: what's interesting about y and z?
x = 1:6
y = x + 1
z = y - 1

sanjay sutar
sanjay sutar le 31 Déc 2022
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 commentaires
Image Analyst
Image Analyst le 31 Déc 2022
Expanding on @Steven Lord's comment above:
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
sanjay sutar le 1 Jan 2023
@steven lord. Thank you very much sir.2nd option worked exactly how I expected.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by