Filter löschen
Filter löschen

Using Randperm between two values

29 Ansichten (letzte 30 Tage)
Danté Davids
Danté Davids am 16 Aug. 2017
Kommentiert: sanjay sutar am 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.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Star Strider
Star Strider am 16 Aug. 2017
Try this:
NextRequirement = 60 + randperm(15, 8)

John BG
John BG am 17 Aug. 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 Kommentare
John BG
John BG am 17 Aug. 2017
Bearbeitet: John BG am 17 Aug. 2017
ok, but lottery systems, by definition, cannot have many winners, can they?
Walter Roberson
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 16 Aug. 2017
Hint: what's interesting about y and z?
x = 1:6
y = x + 1
z = y - 1

sanjay sutar
sanjay sutar am 31 Dez. 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 Kommentare
Image Analyst
Image Analyst am 31 Dez. 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 am 1 Jan. 2023
@steven lord. Thank you very much sir.2nd option worked exactly how I expected.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by