Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 6 Mar 2019
use randperm.
  2 Comments
yang-En Hsiao
yang-En Hsiao on 6 Mar 2019
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 on 6 Mar 2019
randperm as many as needed . unique by rows with stable order option to remove duplicates . go back to generate more if you need to.

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!