フィルターのクリア

Using Randperm between two values

29 ビュー (過去 30 日間)
Danté Davids
Danté Davids 2017 年 8 月 16 日
コメント済み: sanjay sutar 2023 年 1 月 1 日
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 件のコメント
Paul Munro
Paul Munro 2018 年 9 月 3 日
59+randperm(1,16)

サインインしてコメントする。

採用された回答

Geoff Hayes
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.
  1 件のコメント
Danté Davids
Danté Davids 2017 年 8 月 17 日
Ah, it seems so simple now. This really helped me better my understanding of how Matlab works. Thank you.

サインインしてコメントする。

その他の回答 (4 件)

Star Strider
Star Strider 2017 年 8 月 16 日
Try this:
NextRequirement = 60 + randperm(15, 8)

John BG
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 件のコメント
John BG
John BG 2017 年 8 月 17 日
編集済み: John BG 2017 年 8 月 17 日
ok, but lottery systems, by definition, cannot have many winners, can they?
Walter Roberson
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.

サインインしてコメントする。


Steven Lord
Steven Lord 2017 年 8 月 16 日
Hint: what's interesting about y and z?
x = 1:6
y = x + 1
z = y - 1

sanjay sutar
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
Image Analyst 2022 年 12 月 31 日
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 2023 年 1 月 1 日
@steven lord. Thank you very much sir.2nd option worked exactly how I expected.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by