How can I generate random integers between 0 and 9 (inclusive) in Python?
For example, 0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
How can I generate random integers between 0 and 9 (inclusive) in Python?
For example, 0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
In case of continuous numbers
randint
orrandrange
are probably the best choices but if you have several distinct values in a sequence (i.e. alist
) you could also usechoice
:choice
also works for one item from a not-continuous sample:If you need it "cryptographically strong" there's also a
secrets.choice
in python 3.6 and newer:I had better luck with this for Python 3.6
Just add characters like 'ABCD' and 'abcd' or '^!~=-><' to alter the character pool to pull from, change the range to alter the number of characters generated.
Choose the size of the array (in this example, I have chosen the size to be 20). And then, use the following:
You can expect to see an output of the following form (different random integers will be returned each time you run it; hence you can expect the integers in the output array to differ from the example given below).
I used variable to control the range
I used the print function to see the results. You can comment is out if you do not need this.
Try this through
random.shuffle
To get a list of ten samples: