dimanche 10 mars 2019

Lotto code,the previous number cannot appear again,how do i improve it

I use matlab to write this code,and it seems there is something wrong with logic,but i don't know where am i wrong and how to improve this.

i want to write a lotto code,and there are six numbers in it,the range of first six numbers is 1 to 38,the range of last number is 1 to 8.Here is my code

previous_number=randi([1,38],1,6)
last=randi([1,8],1,1) %produce the last number

for k =1:6    
    while  last== previous_number %while that last number is the same as the value of one of the previous number
    last=randi([1,8],1,1)%then produce the last number again,until the different value produce
    end
end
ltto=[previous_number last]

but i found that the last number will still generate the same number as the first six numbers,for example,

"1" 2 33 55 66 10 "1"

1 "2" 33 55 66 10 "2"
Why?i have already said

while  last==previous_number(k)
    last=randi([1,8],1,1)
    end   

if i want to write the code in c or other program language,i think i can just use if ,while and loop,etc,like this basic loop,i can't use the "ismemeber"or randperm. how can i rewrite the code?

if i rewrite as

previous_number=randi([1,38],1,6)
last=randi([1,8],1,1) %produce the last number

for k =1:6    
    if  last== previous_number(k) %while that last number is the same as the value of one of the previous number
    last=randi([1,8],1,1)%then produce the last number again,until the different value produce
    end
end
ltto=[previous_number last]

the result will also show me "1" 2 21 12 13 22 "1" sometimes




Aucun commentaire:

Enregistrer un commentaire