How to write a code to avoid a random number won't be generated again?

1 次查看(过去 30 天)
I want to write a Lotto code,the first six number will come from the first part,there will be 38 numbers,1~38, in the first part.and the last number will come from the second part,and there will be 8 numbers,1~8, in the second parts.The Lotto number will be 6 numbers from first part + 1 number from the second part.However,the Lotti number can't be generated again,i mean if the first Lotto number is 1234567,1234567 can't be generated again.
now i use
first_part_number_p = randi([1 38],1,6)
second_part_number = randi([1 8],1,1)
previous_number=[first_part_number,second_part_number]
Latter_number=[first_part_number,second_part_number]
But i don't know how to write a code to avoid the same code will generated again,does anyone know how to write the code?
now i have two part to generate number.first 6 numbers from 1~38 from the first part,and last 1 number from 1~8 from the second part .so now the lotto number is the 6 numbers from first part + 1 number from the second part.so if this time the first part generate 10 20 30 35 31 12,and the second part generate 10.the lotto number will become 10 20 30 35 31 12 10.And next lotto number can't be 10 20 30 35 31 12 10,if the first part generate 10 20 30 35 31 12,the second part must not generate number "10".
The "randperm " should just generate some value in just one time,and in this time ,the element in this vector won't be the same,but if i generate again,these number may be the same again

回答(1 个)

Walter Roberson
Walter Roberson 2019-3-6
use randperm.
  2 个评论
yang-En Hsiao
yang-En Hsiao 2019-3-6
No,i think you don't understand my meaning,now i have two part to generate number.first 6 numbers from 1~38 from the first part,and last 1 number from 1~8 from the second part .so now the lotto number is the 6 numbers from first part + 1 number from the second part.so if this time the first part generate 10 20 30 35 31 12,and the second part generate 10.the lotto number will become 10 20 30 35 31 12 10.And next lotto number can't be 10 20 30 35 31 12 10,if the first part generate 10 20 30 35 31 12,the second part must not generate number "10".
The "randperm " should just generate some value in just one time,and in this time ,the element in this vector won't be the same,but if i generate again,these number may be the same again
Walter Roberson
Walter Roberson 2019-3-6
randperm as many as needed . unique by rows with stable order option to remove duplicates . go back to generate more if you need to.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by