Jan-30-2018, 01:12 AM
Hello everybody,
I wrote this small piece of code to generate N numbers without repetition.
The algorithm should be fine but since I'm learning Python I'd like to know if my code is correct and if it can be improved with better functions because I have the feeling that it's too much "C style".
I wrote this small piece of code to generate N numbers without repetition.
The algorithm should be fine but since I'm learning Python I'd like to know if my code is correct and if it can be improved with better functions because I have the feeling that it's too much "C style".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import random random.seed() N = 10 array = list ( range ( 1 , N + 1 )) i = 1 a_size = N while a_size > 1 : p = random.getrandbits( 32 ) k = p % a_size print (array[k]) array[k] = array[a_size - 1 ] a_size = a_size - 1 print (array[ 0 ]) |