for example, there are 100 numbers from 1 to 100, each number must be issued randomly without repeating
2 answers
import random l = list(range(1, 101)) random.shuffle(l) for i in l: print(i) - The letter
leasy to confuse. BetterLuse. - jfs - For what if not a secret range turns into a list? Like the range and so the list returns. - Vladimir Gamalyan
- In python 3, the range returns a generator. - Sergey Gornostaev
- Exactly, thank you, I did not think about the 3rd branch. - Vladimir Gamalyan
|
One option is to use a linear congruential pseudo-random number generator . For example, to get all the numbers from 0 to 99, use the initial approximation - any x, for example, x=17 and then call the procedure x = (21*x+1) mod 100 hundred times. It is clear that to get all the numbers from 1 to 100, you need to add one to the sequence, that is, at each step, do y[i]=x+1 to get an array of y [] with the desired property. More precisely, the selection of the coefficients of the linear generator is considered by reference.
The second way is to create an array of numbers from 1 to 100 and randomly mix it.
|
l = range(1, 101)then mix it up:random.shuffle(l)- Vladimir Gamalyan