lundi 21 mars 2016

block repeat of random only for certain step through the random value

random.shuffle or random.sample or random.choice can work for this solution but not exactly like this, it would be good to understand how to solve this just through the random number value

need two things here

import random
import time

x = 1
while True:
    print ("%d " % (x))
    ran = (random.randrange(0,11))
    print ('Random :', ran)
    x += 1
    time.sleep(1)

how to avoid repeat for each random number in endless loop diapason but only for next one step, for example:

1 
Random : 5
2 
Random : 8 # only for this step do not allow repeat of previous 5 
3 
Random : 10 # only for this step do not allow repeat of previous 8
4 
Random : 7 # only for this step do not allow repeat of previous 10 
5 
Random : 10 #only for current step do not allow repeat of previous 7 
6 
Random : 7 # only for current step do not allow repeat of previous 10 

so I want here, if previous number is equal to current, generate new until it becomes different, if different, print it as next, forget previous block and block current for next one step only etc. in other words block repeat for current if previous is same.

but now if I want block repeat for each number through several steps, avoid repeat of current only through one step, on step 3 for each current, or any certain most distant step after current, for example:

1 
Random : 5 
2 
Random : 8 
3 
Random : 10 #for this step only do not allow repeat of 5
4 
Random : 10 #for this step only do not allow repeat of 8 
5 
Random : 5 #for current step only do not allow repeat of 10 
6 
Random : 4 #for current step only do not allow repeat of 10

or for each on different distance:

1 
Random : 9     #avoid repeat of this number after 2 step from current  
2 
Random : 6           #avoid repeat of number after 3 step from current  
3 
Random : 10 
4
Random : 7     #only for this step do not allow repeat of 9 
5 
Random : 5 
6 
Random : 4           #only for this step  do not allow repeat of 6

and at the same time, for example if in diapason 0-10 I want fix some defined number for example 3, if random generates it, only in this case I want print "was number 3!"

1 
Random : 5
2 
Random : 8 
3 
Random : 3 was number 3! 
4 
Random : 7 
5 
Random : 10  
6 
Random : 3 was number 3!




Aucun commentaire:

Enregistrer un commentaire