Generate random integers between 0 and 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
python random integer
add a comment |
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
python random integer
add a comment |
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
python random integer
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
python random integer
python random integer
edited Oct 18 '18 at 8:08


K48
5,62594096
5,62594096
asked Oct 22 '10 at 12:48
aneuryzmaneuryzm
21k87238448
21k87238448
add a comment |
add a comment |
21 Answers
21
active
oldest
votes
Try:
from random import randint
print(randint(0, 9))
More info: https://docs.python.org/3/library/random.html#random.randint
21
Just a note, these are pseudorandom numbers and they are not cryptographically secure. Do not use this in any case where you don't want an attacker to guess your numbers. Use thesecrets
module for better random numbers. Reference: docs.python.org/3/library/random.html
– K48
Oct 18 '18 at 8:08
add a comment |
import random
print(random.randint(0,9))
random.randint(a, b)
Return a random integer N such that a <= N <= b.
Docs: https://docs.python.org/3.1/library/random.html#random.randint
add a comment |
Try this:
from random import randrange, uniform
# randrange gives you an integral value
irand = randrange(0, 10)
# uniform gives you a floating-point value
frand = uniform(0, 10)
add a comment |
from random import randint
x = [randint(0, 9) for p in range(0, 10)]
This generates 10 pseudorandom integers in range 0 to 9 inclusive.
3
10 or 9? I'm getting only 9.
– Sigur
May 11 '17 at 13:52
add a comment |
The secrets
module is new in Python 3.6. This is better than the random
module for cryptography or security uses.
To randomly print an integer in the inclusive range 0-9:
from secrets import randbelow
print(randbelow(10))
For details, see PEP 506.
1
This would improve the answer and should be added. The more security minded answers should always be added if available.
– SudoKid
Feb 7 '18 at 17:15
add a comment |
Try this through random.shuffle
>>> import random
>>> nums = [x for x in range(10)]
>>> random.shuffle(nums)
>>> nums
[6, 3, 5, 4, 0, 1, 2, 9, 8, 7]
3
nums = range(10)
– Kamejoin
Dec 14 '17 at 20:10
add a comment |
Choose the size of the array (in this example, I have chosen the size to be 20). And then, use the following:
import numpy as np
np.random.randint(10, size=(1, 20))
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).
array([[1, 6, 1, 2, 8, 6, 3, 3, 2, 5, 6, 5, 0, 9, 5, 6, 4, 5, 9, 3]])
2
It's also helpful to know how Numpy can generate a random array of specified size, not just a single random number. (Docs: numpy.random.randint)
– jkdev
Jun 25 '17 at 18:19
add a comment |
In case of continuous numbers randint
or randrange
are probably the best choices but if you have several distinct values in a sequence (i.e. a list
) you could also use choice
:
>>> import random
>>> values = list(range(10))
>>> random.choice(values)
5
choice
also works for one item from a not-continuous sample:
>>> values = [1, 2, 3, 5, 7, 10]
>>> random.choice(values)
7
If you need it "cryptographically strong" there's also a secrets.choice
in python 3.6 and newer:
>>> import secrets
>>> values = list(range(10))
>>> secrets.choice(values)
2
What if we want more numbers from the sequence?
– Gunjan naik
Oct 3 '17 at 13:43
If they should be without replacement:random.sample
. With replacement you could use a comprehension withchoice
: for example for a list containing 3 random values with replacement:[choice(values) for _ in range(3)]
– MSeifert
Oct 3 '17 at 13:53
add a comment |
if you want to use numpy then use the following:
import numpy as np
print(np.random.randint(0,10))
1
You could tell something about "numpy".
– Simón
Jan 20 '17 at 4:09
2
numpy.org
– sushmit
Jan 20 '17 at 5:57
9
Yeah. Thanks for the link. But I intended to mean that you could have improved your answer by providing details before just quoting two lines of code; like for what reason would someone prefer to use it instead of something already built in. Not that you're obliged to, anyway.
– Simón
Jan 20 '17 at 16:00
add a comment |
The original question implies generating multiple random integers.
How can I generate integers between 0 and 9 (inclusive) in Python?
Many responses however only show how to get one random number, e.g. random.randint
and random.choice
.
Multiple Random Integers
For clarity, you can still generate multiple random numbers using those techniques by simply iterating N
times:
import random
N = 5
[random.randint(0, 9) for _ in range(N)]
# [9, 7, 0, 7, 3]
[random.choice(range(10)) for _ in range(N)]
# [8, 3, 6, 8, 7]
Sample of Random Integers
Some posts demonstrate how to natively generate multiple random integers.1 Here are some options that address the implied question:
random.sample
returns k
unique selections from a population (without replacement):2
random.sample(range(10), k=N)
# [4, 5, 1, 2, 3]
In Python 3.6, random.choices
returns k
selections from a population (with replacement):
random.choices(range(10), k=N)
# [3, 2, 0, 8, 2]
See also this related post using numpy.random.choice
.
1Namely @John Lawrence Aspden, @S T Mohammed, @SiddTheKid, @user14372, @zangw, et al.
2@prashanth mentions this module showing one integer.
add a comment |
>>> import random
>>> random.randrange(10)
3
>>> random.randrange(10)
1
To get a list of ten samples:
>>> [random.randrange(10) for x in range(10)]
[9, 0, 4, 0, 5, 7, 4, 3, 6, 8]
add a comment |
random.sample
is another that can be used
import random
n = 1 # specify the no. of numbers
num = random.sample(range(10), n)
num[0] # is the required number
add a comment |
Generating random integers between 0 and 9.
import numpy
X = numpy.random.randint(0, 10, size=10)
print(X)
Output:
[4 8 0 4 9 6 9 9 0 7]
1
If you're using NumPy, you should use NumPy's random functionalities to generate this list, for example withnumpy.random.randint(0, 10, size=10)
. The method you show is needlessly inefficient.
– Mark Dickinson
Jul 6 '18 at 6:44
add a comment |
Best way is to use import Random function
import random
print(random.sample(range(10), 10))
or without any library import:
n={}
for i in range(10):
n[i]=i
for p in range(10):
print(n.popitem()[1])
here the popitems removes and returns an arbitrary value from the dictionary n
.
add a comment |
You can try this:
import numpy as np
print ( np.random.uniform(low=0, high=10, size=(15,)) ).astype(int)
>>> [8 3 6 9 1 0 3 6 3 3 1 2 4 0 4]
Notes:
1.> np.random.uniform generates uniformly distributed numbers over the half-open interval [low, high).
2.> astype(int) casts the numpy array to int data type.
3.> I have chosen size = (15,). This will give you a numpy array of length = 15.
More information on numpy.random.uniform
More information on numpy.ndarray.astype
add a comment |
I used variable to control the range
from random import randint
numberStartRange = 1
numberEndRange = 9
randomNumber = randint(numberStartRange, numberEndRange)
print(randomNumber)
I used the print function to see the results. You can comment is out if you do not need this.
add a comment |
This is more of a mathematical approach but it works 100% of the time:
Let's say you want to use random.random()
function to generate a number between a
and b
. To achieve this, just do the following:
num = (b-a)*random.random() + a;
Of course, you can generate more numbers.
add a comment |
For the example that you have given (a random integer between 0 and 9), the cleanest solution is:
from random import randrange
randrange(10)
randrange returns a single number between specified range.
– Shital Shah
Jan 2 at 1:43
@ShitalShah That is exactly what the question wants. Run the accepted answer and realize that it does the same thing.
– Utku
Jan 2 at 2:06
@ShitalShah If you want all numbers between 0 and 9, you userange(10)
.
– Utku
Jan 2 at 2:07
add a comment |
Try This,
import numpy as np
X = np.random.randint(0, 99, size=1000) # 1k random integer
add a comment |
From the documentation page for the random module:
Warning: The pseudo-random generators of this module should not be
used for security purposes. Use os.urandom() or SystemRandom if you
require a cryptographically secure pseudo-random number generator.
random.SystemRandom, which was introduced in Python 2.4, is considered cryptographically secure. It is still available in Python 3.7.1 which is current at time of writing.
>>> import string
>>> string.digits
'0123456789'
>>> import random
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'1'
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'5'
Instead of string.digits
, range
could be used per some of the other answers along perhaps with a comprehension. Mix and match according to your needs.
add a comment |
I had better luck with this for Python 3.6
str_Key = ""
str_RandomKey = ""
for int_I in range(128):
str_Key = random.choice('0123456789')
str_RandomKey = str_RandomKey + str_Key
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.
add a comment |
protected by Aniket Thakur Sep 9 '15 at 8:43
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
21 Answers
21
active
oldest
votes
21 Answers
21
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try:
from random import randint
print(randint(0, 9))
More info: https://docs.python.org/3/library/random.html#random.randint
21
Just a note, these are pseudorandom numbers and they are not cryptographically secure. Do not use this in any case where you don't want an attacker to guess your numbers. Use thesecrets
module for better random numbers. Reference: docs.python.org/3/library/random.html
– K48
Oct 18 '18 at 8:08
add a comment |
Try:
from random import randint
print(randint(0, 9))
More info: https://docs.python.org/3/library/random.html#random.randint
21
Just a note, these are pseudorandom numbers and they are not cryptographically secure. Do not use this in any case where you don't want an attacker to guess your numbers. Use thesecrets
module for better random numbers. Reference: docs.python.org/3/library/random.html
– K48
Oct 18 '18 at 8:08
add a comment |
Try:
from random import randint
print(randint(0, 9))
More info: https://docs.python.org/3/library/random.html#random.randint
Try:
from random import randint
print(randint(0, 9))
More info: https://docs.python.org/3/library/random.html#random.randint
edited Jun 11 '17 at 21:38


A-B-B
23.9k66370
23.9k66370
answered Oct 22 '10 at 12:51
kovsheninkovshenin
21.7k43042
21.7k43042
21
Just a note, these are pseudorandom numbers and they are not cryptographically secure. Do not use this in any case where you don't want an attacker to guess your numbers. Use thesecrets
module for better random numbers. Reference: docs.python.org/3/library/random.html
– K48
Oct 18 '18 at 8:08
add a comment |
21
Just a note, these are pseudorandom numbers and they are not cryptographically secure. Do not use this in any case where you don't want an attacker to guess your numbers. Use thesecrets
module for better random numbers. Reference: docs.python.org/3/library/random.html
– K48
Oct 18 '18 at 8:08
21
21
Just a note, these are pseudorandom numbers and they are not cryptographically secure. Do not use this in any case where you don't want an attacker to guess your numbers. Use the
secrets
module for better random numbers. Reference: docs.python.org/3/library/random.html– K48
Oct 18 '18 at 8:08
Just a note, these are pseudorandom numbers and they are not cryptographically secure. Do not use this in any case where you don't want an attacker to guess your numbers. Use the
secrets
module for better random numbers. Reference: docs.python.org/3/library/random.html– K48
Oct 18 '18 at 8:08
add a comment |
import random
print(random.randint(0,9))
random.randint(a, b)
Return a random integer N such that a <= N <= b.
Docs: https://docs.python.org/3.1/library/random.html#random.randint
add a comment |
import random
print(random.randint(0,9))
random.randint(a, b)
Return a random integer N such that a <= N <= b.
Docs: https://docs.python.org/3.1/library/random.html#random.randint
add a comment |
import random
print(random.randint(0,9))
random.randint(a, b)
Return a random integer N such that a <= N <= b.
Docs: https://docs.python.org/3.1/library/random.html#random.randint
import random
print(random.randint(0,9))
random.randint(a, b)
Return a random integer N such that a <= N <= b.
Docs: https://docs.python.org/3.1/library/random.html#random.randint
edited Jun 30 '15 at 14:51
FlipperPA
7,23622145
7,23622145
answered May 4 '13 at 17:13
JMSamudioJMSamudio
3,307178
3,307178
add a comment |
add a comment |
Try this:
from random import randrange, uniform
# randrange gives you an integral value
irand = randrange(0, 10)
# uniform gives you a floating-point value
frand = uniform(0, 10)
add a comment |
Try this:
from random import randrange, uniform
# randrange gives you an integral value
irand = randrange(0, 10)
# uniform gives you a floating-point value
frand = uniform(0, 10)
add a comment |
Try this:
from random import randrange, uniform
# randrange gives you an integral value
irand = randrange(0, 10)
# uniform gives you a floating-point value
frand = uniform(0, 10)
Try this:
from random import randrange, uniform
# randrange gives you an integral value
irand = randrange(0, 10)
# uniform gives you a floating-point value
frand = uniform(0, 10)
edited Oct 22 '10 at 12:55
answered Oct 22 '10 at 12:49


Andrew HareAndrew Hare
279k54581602
279k54581602
add a comment |
add a comment |
from random import randint
x = [randint(0, 9) for p in range(0, 10)]
This generates 10 pseudorandom integers in range 0 to 9 inclusive.
3
10 or 9? I'm getting only 9.
– Sigur
May 11 '17 at 13:52
add a comment |
from random import randint
x = [randint(0, 9) for p in range(0, 10)]
This generates 10 pseudorandom integers in range 0 to 9 inclusive.
3
10 or 9? I'm getting only 9.
– Sigur
May 11 '17 at 13:52
add a comment |
from random import randint
x = [randint(0, 9) for p in range(0, 10)]
This generates 10 pseudorandom integers in range 0 to 9 inclusive.
from random import randint
x = [randint(0, 9) for p in range(0, 10)]
This generates 10 pseudorandom integers in range 0 to 9 inclusive.
edited May 17 '18 at 8:05
Community♦
11
11
answered Nov 26 '13 at 17:39
user14372user14372
821711
821711
3
10 or 9? I'm getting only 9.
– Sigur
May 11 '17 at 13:52
add a comment |
3
10 or 9? I'm getting only 9.
– Sigur
May 11 '17 at 13:52
3
3
10 or 9? I'm getting only 9.
– Sigur
May 11 '17 at 13:52
10 or 9? I'm getting only 9.
– Sigur
May 11 '17 at 13:52
add a comment |
The secrets
module is new in Python 3.6. This is better than the random
module for cryptography or security uses.
To randomly print an integer in the inclusive range 0-9:
from secrets import randbelow
print(randbelow(10))
For details, see PEP 506.
1
This would improve the answer and should be added. The more security minded answers should always be added if available.
– SudoKid
Feb 7 '18 at 17:15
add a comment |
The secrets
module is new in Python 3.6. This is better than the random
module for cryptography or security uses.
To randomly print an integer in the inclusive range 0-9:
from secrets import randbelow
print(randbelow(10))
For details, see PEP 506.
1
This would improve the answer and should be added. The more security minded answers should always be added if available.
– SudoKid
Feb 7 '18 at 17:15
add a comment |
The secrets
module is new in Python 3.6. This is better than the random
module for cryptography or security uses.
To randomly print an integer in the inclusive range 0-9:
from secrets import randbelow
print(randbelow(10))
For details, see PEP 506.
The secrets
module is new in Python 3.6. This is better than the random
module for cryptography or security uses.
To randomly print an integer in the inclusive range 0-9:
from secrets import randbelow
print(randbelow(10))
For details, see PEP 506.
edited Dec 6 '16 at 10:40
answered Dec 6 '16 at 10:29
Chris_RandsChris_Rands
17k54176
17k54176
1
This would improve the answer and should be added. The more security minded answers should always be added if available.
– SudoKid
Feb 7 '18 at 17:15
add a comment |
1
This would improve the answer and should be added. The more security minded answers should always be added if available.
– SudoKid
Feb 7 '18 at 17:15
1
1
This would improve the answer and should be added. The more security minded answers should always be added if available.
– SudoKid
Feb 7 '18 at 17:15
This would improve the answer and should be added. The more security minded answers should always be added if available.
– SudoKid
Feb 7 '18 at 17:15
add a comment |
Try this through random.shuffle
>>> import random
>>> nums = [x for x in range(10)]
>>> random.shuffle(nums)
>>> nums
[6, 3, 5, 4, 0, 1, 2, 9, 8, 7]
3
nums = range(10)
– Kamejoin
Dec 14 '17 at 20:10
add a comment |
Try this through random.shuffle
>>> import random
>>> nums = [x for x in range(10)]
>>> random.shuffle(nums)
>>> nums
[6, 3, 5, 4, 0, 1, 2, 9, 8, 7]
3
nums = range(10)
– Kamejoin
Dec 14 '17 at 20:10
add a comment |
Try this through random.shuffle
>>> import random
>>> nums = [x for x in range(10)]
>>> random.shuffle(nums)
>>> nums
[6, 3, 5, 4, 0, 1, 2, 9, 8, 7]
Try this through random.shuffle
>>> import random
>>> nums = [x for x in range(10)]
>>> random.shuffle(nums)
>>> nums
[6, 3, 5, 4, 0, 1, 2, 9, 8, 7]
edited Dec 10 '15 at 17:57
Stophface
2,58273080
2,58273080
answered Oct 29 '15 at 9:13


zangwzangw
24k795116
24k795116
3
nums = range(10)
– Kamejoin
Dec 14 '17 at 20:10
add a comment |
3
nums = range(10)
– Kamejoin
Dec 14 '17 at 20:10
3
3
nums = range(10)
– Kamejoin
Dec 14 '17 at 20:10
nums = range(10)
– Kamejoin
Dec 14 '17 at 20:10
add a comment |
Choose the size of the array (in this example, I have chosen the size to be 20). And then, use the following:
import numpy as np
np.random.randint(10, size=(1, 20))
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).
array([[1, 6, 1, 2, 8, 6, 3, 3, 2, 5, 6, 5, 0, 9, 5, 6, 4, 5, 9, 3]])
2
It's also helpful to know how Numpy can generate a random array of specified size, not just a single random number. (Docs: numpy.random.randint)
– jkdev
Jun 25 '17 at 18:19
add a comment |
Choose the size of the array (in this example, I have chosen the size to be 20). And then, use the following:
import numpy as np
np.random.randint(10, size=(1, 20))
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).
array([[1, 6, 1, 2, 8, 6, 3, 3, 2, 5, 6, 5, 0, 9, 5, 6, 4, 5, 9, 3]])
2
It's also helpful to know how Numpy can generate a random array of specified size, not just a single random number. (Docs: numpy.random.randint)
– jkdev
Jun 25 '17 at 18:19
add a comment |
Choose the size of the array (in this example, I have chosen the size to be 20). And then, use the following:
import numpy as np
np.random.randint(10, size=(1, 20))
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).
array([[1, 6, 1, 2, 8, 6, 3, 3, 2, 5, 6, 5, 0, 9, 5, 6, 4, 5, 9, 3]])
Choose the size of the array (in this example, I have chosen the size to be 20). And then, use the following:
import numpy as np
np.random.randint(10, size=(1, 20))
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).
array([[1, 6, 1, 2, 8, 6, 3, 3, 2, 5, 6, 5, 0, 9, 5, 6, 4, 5, 9, 3]])
answered Feb 2 '17 at 6:35


CommonerCommoner
549820
549820
2
It's also helpful to know how Numpy can generate a random array of specified size, not just a single random number. (Docs: numpy.random.randint)
– jkdev
Jun 25 '17 at 18:19
add a comment |
2
It's also helpful to know how Numpy can generate a random array of specified size, not just a single random number. (Docs: numpy.random.randint)
– jkdev
Jun 25 '17 at 18:19
2
2
It's also helpful to know how Numpy can generate a random array of specified size, not just a single random number. (Docs: numpy.random.randint)
– jkdev
Jun 25 '17 at 18:19
It's also helpful to know how Numpy can generate a random array of specified size, not just a single random number. (Docs: numpy.random.randint)
– jkdev
Jun 25 '17 at 18:19
add a comment |
In case of continuous numbers randint
or randrange
are probably the best choices but if you have several distinct values in a sequence (i.e. a list
) you could also use choice
:
>>> import random
>>> values = list(range(10))
>>> random.choice(values)
5
choice
also works for one item from a not-continuous sample:
>>> values = [1, 2, 3, 5, 7, 10]
>>> random.choice(values)
7
If you need it "cryptographically strong" there's also a secrets.choice
in python 3.6 and newer:
>>> import secrets
>>> values = list(range(10))
>>> secrets.choice(values)
2
What if we want more numbers from the sequence?
– Gunjan naik
Oct 3 '17 at 13:43
If they should be without replacement:random.sample
. With replacement you could use a comprehension withchoice
: for example for a list containing 3 random values with replacement:[choice(values) for _ in range(3)]
– MSeifert
Oct 3 '17 at 13:53
add a comment |
In case of continuous numbers randint
or randrange
are probably the best choices but if you have several distinct values in a sequence (i.e. a list
) you could also use choice
:
>>> import random
>>> values = list(range(10))
>>> random.choice(values)
5
choice
also works for one item from a not-continuous sample:
>>> values = [1, 2, 3, 5, 7, 10]
>>> random.choice(values)
7
If you need it "cryptographically strong" there's also a secrets.choice
in python 3.6 and newer:
>>> import secrets
>>> values = list(range(10))
>>> secrets.choice(values)
2
What if we want more numbers from the sequence?
– Gunjan naik
Oct 3 '17 at 13:43
If they should be without replacement:random.sample
. With replacement you could use a comprehension withchoice
: for example for a list containing 3 random values with replacement:[choice(values) for _ in range(3)]
– MSeifert
Oct 3 '17 at 13:53
add a comment |
In case of continuous numbers randint
or randrange
are probably the best choices but if you have several distinct values in a sequence (i.e. a list
) you could also use choice
:
>>> import random
>>> values = list(range(10))
>>> random.choice(values)
5
choice
also works for one item from a not-continuous sample:
>>> values = [1, 2, 3, 5, 7, 10]
>>> random.choice(values)
7
If you need it "cryptographically strong" there's also a secrets.choice
in python 3.6 and newer:
>>> import secrets
>>> values = list(range(10))
>>> secrets.choice(values)
2
In case of continuous numbers randint
or randrange
are probably the best choices but if you have several distinct values in a sequence (i.e. a list
) you could also use choice
:
>>> import random
>>> values = list(range(10))
>>> random.choice(values)
5
choice
also works for one item from a not-continuous sample:
>>> values = [1, 2, 3, 5, 7, 10]
>>> random.choice(values)
7
If you need it "cryptographically strong" there's also a secrets.choice
in python 3.6 and newer:
>>> import secrets
>>> values = list(range(10))
>>> secrets.choice(values)
2
edited Sep 24 '17 at 10:33
answered Jun 3 '17 at 1:45
MSeifertMSeifert
77.3k18148185
77.3k18148185
What if we want more numbers from the sequence?
– Gunjan naik
Oct 3 '17 at 13:43
If they should be without replacement:random.sample
. With replacement you could use a comprehension withchoice
: for example for a list containing 3 random values with replacement:[choice(values) for _ in range(3)]
– MSeifert
Oct 3 '17 at 13:53
add a comment |
What if we want more numbers from the sequence?
– Gunjan naik
Oct 3 '17 at 13:43
If they should be without replacement:random.sample
. With replacement you could use a comprehension withchoice
: for example for a list containing 3 random values with replacement:[choice(values) for _ in range(3)]
– MSeifert
Oct 3 '17 at 13:53
What if we want more numbers from the sequence?
– Gunjan naik
Oct 3 '17 at 13:43
What if we want more numbers from the sequence?
– Gunjan naik
Oct 3 '17 at 13:43
If they should be without replacement:
random.sample
. With replacement you could use a comprehension with choice
: for example for a list containing 3 random values with replacement: [choice(values) for _ in range(3)]
– MSeifert
Oct 3 '17 at 13:53
If they should be without replacement:
random.sample
. With replacement you could use a comprehension with choice
: for example for a list containing 3 random values with replacement: [choice(values) for _ in range(3)]
– MSeifert
Oct 3 '17 at 13:53
add a comment |
if you want to use numpy then use the following:
import numpy as np
print(np.random.randint(0,10))
1
You could tell something about "numpy".
– Simón
Jan 20 '17 at 4:09
2
numpy.org
– sushmit
Jan 20 '17 at 5:57
9
Yeah. Thanks for the link. But I intended to mean that you could have improved your answer by providing details before just quoting two lines of code; like for what reason would someone prefer to use it instead of something already built in. Not that you're obliged to, anyway.
– Simón
Jan 20 '17 at 16:00
add a comment |
if you want to use numpy then use the following:
import numpy as np
print(np.random.randint(0,10))
1
You could tell something about "numpy".
– Simón
Jan 20 '17 at 4:09
2
numpy.org
– sushmit
Jan 20 '17 at 5:57
9
Yeah. Thanks for the link. But I intended to mean that you could have improved your answer by providing details before just quoting two lines of code; like for what reason would someone prefer to use it instead of something already built in. Not that you're obliged to, anyway.
– Simón
Jan 20 '17 at 16:00
add a comment |
if you want to use numpy then use the following:
import numpy as np
print(np.random.randint(0,10))
if you want to use numpy then use the following:
import numpy as np
print(np.random.randint(0,10))
edited Jun 3 '17 at 1:39
MSeifert
77.3k18148185
77.3k18148185
answered Jan 12 '17 at 3:12


sushmitsushmit
1,89711825
1,89711825
1
You could tell something about "numpy".
– Simón
Jan 20 '17 at 4:09
2
numpy.org
– sushmit
Jan 20 '17 at 5:57
9
Yeah. Thanks for the link. But I intended to mean that you could have improved your answer by providing details before just quoting two lines of code; like for what reason would someone prefer to use it instead of something already built in. Not that you're obliged to, anyway.
– Simón
Jan 20 '17 at 16:00
add a comment |
1
You could tell something about "numpy".
– Simón
Jan 20 '17 at 4:09
2
numpy.org
– sushmit
Jan 20 '17 at 5:57
9
Yeah. Thanks for the link. But I intended to mean that you could have improved your answer by providing details before just quoting two lines of code; like for what reason would someone prefer to use it instead of something already built in. Not that you're obliged to, anyway.
– Simón
Jan 20 '17 at 16:00
1
1
You could tell something about "numpy".
– Simón
Jan 20 '17 at 4:09
You could tell something about "numpy".
– Simón
Jan 20 '17 at 4:09
2
2
numpy.org
– sushmit
Jan 20 '17 at 5:57
numpy.org
– sushmit
Jan 20 '17 at 5:57
9
9
Yeah. Thanks for the link. But I intended to mean that you could have improved your answer by providing details before just quoting two lines of code; like for what reason would someone prefer to use it instead of something already built in. Not that you're obliged to, anyway.
– Simón
Jan 20 '17 at 16:00
Yeah. Thanks for the link. But I intended to mean that you could have improved your answer by providing details before just quoting two lines of code; like for what reason would someone prefer to use it instead of something already built in. Not that you're obliged to, anyway.
– Simón
Jan 20 '17 at 16:00
add a comment |
The original question implies generating multiple random integers.
How can I generate integers between 0 and 9 (inclusive) in Python?
Many responses however only show how to get one random number, e.g. random.randint
and random.choice
.
Multiple Random Integers
For clarity, you can still generate multiple random numbers using those techniques by simply iterating N
times:
import random
N = 5
[random.randint(0, 9) for _ in range(N)]
# [9, 7, 0, 7, 3]
[random.choice(range(10)) for _ in range(N)]
# [8, 3, 6, 8, 7]
Sample of Random Integers
Some posts demonstrate how to natively generate multiple random integers.1 Here are some options that address the implied question:
random.sample
returns k
unique selections from a population (without replacement):2
random.sample(range(10), k=N)
# [4, 5, 1, 2, 3]
In Python 3.6, random.choices
returns k
selections from a population (with replacement):
random.choices(range(10), k=N)
# [3, 2, 0, 8, 2]
See also this related post using numpy.random.choice
.
1Namely @John Lawrence Aspden, @S T Mohammed, @SiddTheKid, @user14372, @zangw, et al.
2@prashanth mentions this module showing one integer.
add a comment |
The original question implies generating multiple random integers.
How can I generate integers between 0 and 9 (inclusive) in Python?
Many responses however only show how to get one random number, e.g. random.randint
and random.choice
.
Multiple Random Integers
For clarity, you can still generate multiple random numbers using those techniques by simply iterating N
times:
import random
N = 5
[random.randint(0, 9) for _ in range(N)]
# [9, 7, 0, 7, 3]
[random.choice(range(10)) for _ in range(N)]
# [8, 3, 6, 8, 7]
Sample of Random Integers
Some posts demonstrate how to natively generate multiple random integers.1 Here are some options that address the implied question:
random.sample
returns k
unique selections from a population (without replacement):2
random.sample(range(10), k=N)
# [4, 5, 1, 2, 3]
In Python 3.6, random.choices
returns k
selections from a population (with replacement):
random.choices(range(10), k=N)
# [3, 2, 0, 8, 2]
See also this related post using numpy.random.choice
.
1Namely @John Lawrence Aspden, @S T Mohammed, @SiddTheKid, @user14372, @zangw, et al.
2@prashanth mentions this module showing one integer.
add a comment |
The original question implies generating multiple random integers.
How can I generate integers between 0 and 9 (inclusive) in Python?
Many responses however only show how to get one random number, e.g. random.randint
and random.choice
.
Multiple Random Integers
For clarity, you can still generate multiple random numbers using those techniques by simply iterating N
times:
import random
N = 5
[random.randint(0, 9) for _ in range(N)]
# [9, 7, 0, 7, 3]
[random.choice(range(10)) for _ in range(N)]
# [8, 3, 6, 8, 7]
Sample of Random Integers
Some posts demonstrate how to natively generate multiple random integers.1 Here are some options that address the implied question:
random.sample
returns k
unique selections from a population (without replacement):2
random.sample(range(10), k=N)
# [4, 5, 1, 2, 3]
In Python 3.6, random.choices
returns k
selections from a population (with replacement):
random.choices(range(10), k=N)
# [3, 2, 0, 8, 2]
See also this related post using numpy.random.choice
.
1Namely @John Lawrence Aspden, @S T Mohammed, @SiddTheKid, @user14372, @zangw, et al.
2@prashanth mentions this module showing one integer.
The original question implies generating multiple random integers.
How can I generate integers between 0 and 9 (inclusive) in Python?
Many responses however only show how to get one random number, e.g. random.randint
and random.choice
.
Multiple Random Integers
For clarity, you can still generate multiple random numbers using those techniques by simply iterating N
times:
import random
N = 5
[random.randint(0, 9) for _ in range(N)]
# [9, 7, 0, 7, 3]
[random.choice(range(10)) for _ in range(N)]
# [8, 3, 6, 8, 7]
Sample of Random Integers
Some posts demonstrate how to natively generate multiple random integers.1 Here are some options that address the implied question:
random.sample
returns k
unique selections from a population (without replacement):2
random.sample(range(10), k=N)
# [4, 5, 1, 2, 3]
In Python 3.6, random.choices
returns k
selections from a population (with replacement):
random.choices(range(10), k=N)
# [3, 2, 0, 8, 2]
See also this related post using numpy.random.choice
.
1Namely @John Lawrence Aspden, @S T Mohammed, @SiddTheKid, @user14372, @zangw, et al.
2@prashanth mentions this module showing one integer.
edited Nov 16 '17 at 4:37
answered Sep 22 '17 at 4:29


pylangpylang
14.1k24458
14.1k24458
add a comment |
add a comment |
>>> import random
>>> random.randrange(10)
3
>>> random.randrange(10)
1
To get a list of ten samples:
>>> [random.randrange(10) for x in range(10)]
[9, 0, 4, 0, 5, 7, 4, 3, 6, 8]
add a comment |
>>> import random
>>> random.randrange(10)
3
>>> random.randrange(10)
1
To get a list of ten samples:
>>> [random.randrange(10) for x in range(10)]
[9, 0, 4, 0, 5, 7, 4, 3, 6, 8]
add a comment |
>>> import random
>>> random.randrange(10)
3
>>> random.randrange(10)
1
To get a list of ten samples:
>>> [random.randrange(10) for x in range(10)]
[9, 0, 4, 0, 5, 7, 4, 3, 6, 8]
>>> import random
>>> random.randrange(10)
3
>>> random.randrange(10)
1
To get a list of ten samples:
>>> [random.randrange(10) for x in range(10)]
[9, 0, 4, 0, 5, 7, 4, 3, 6, 8]
answered Aug 10 '17 at 15:48
John Lawrence AspdenJohn Lawrence Aspden
11.1k105190
11.1k105190
add a comment |
add a comment |
random.sample
is another that can be used
import random
n = 1 # specify the no. of numbers
num = random.sample(range(10), n)
num[0] # is the required number
add a comment |
random.sample
is another that can be used
import random
n = 1 # specify the no. of numbers
num = random.sample(range(10), n)
num[0] # is the required number
add a comment |
random.sample
is another that can be used
import random
n = 1 # specify the no. of numbers
num = random.sample(range(10), n)
num[0] # is the required number
random.sample
is another that can be used
import random
n = 1 # specify the no. of numbers
num = random.sample(range(10), n)
num[0] # is the required number
answered May 14 '17 at 16:46
prashanthprashanth
1,7061227
1,7061227
add a comment |
add a comment |
Generating random integers between 0 and 9.
import numpy
X = numpy.random.randint(0, 10, size=10)
print(X)
Output:
[4 8 0 4 9 6 9 9 0 7]
1
If you're using NumPy, you should use NumPy's random functionalities to generate this list, for example withnumpy.random.randint(0, 10, size=10)
. The method you show is needlessly inefficient.
– Mark Dickinson
Jul 6 '18 at 6:44
add a comment |
Generating random integers between 0 and 9.
import numpy
X = numpy.random.randint(0, 10, size=10)
print(X)
Output:
[4 8 0 4 9 6 9 9 0 7]
1
If you're using NumPy, you should use NumPy's random functionalities to generate this list, for example withnumpy.random.randint(0, 10, size=10)
. The method you show is needlessly inefficient.
– Mark Dickinson
Jul 6 '18 at 6:44
add a comment |
Generating random integers between 0 and 9.
import numpy
X = numpy.random.randint(0, 10, size=10)
print(X)
Output:
[4 8 0 4 9 6 9 9 0 7]
Generating random integers between 0 and 9.
import numpy
X = numpy.random.randint(0, 10, size=10)
print(X)
Output:
[4 8 0 4 9 6 9 9 0 7]
edited Jul 6 '18 at 14:11
answered Jul 6 '18 at 6:25
Ashok Kumar JayaramanAshok Kumar Jayaraman
1,09711119
1,09711119
1
If you're using NumPy, you should use NumPy's random functionalities to generate this list, for example withnumpy.random.randint(0, 10, size=10)
. The method you show is needlessly inefficient.
– Mark Dickinson
Jul 6 '18 at 6:44
add a comment |
1
If you're using NumPy, you should use NumPy's random functionalities to generate this list, for example withnumpy.random.randint(0, 10, size=10)
. The method you show is needlessly inefficient.
– Mark Dickinson
Jul 6 '18 at 6:44
1
1
If you're using NumPy, you should use NumPy's random functionalities to generate this list, for example with
numpy.random.randint(0, 10, size=10)
. The method you show is needlessly inefficient.– Mark Dickinson
Jul 6 '18 at 6:44
If you're using NumPy, you should use NumPy's random functionalities to generate this list, for example with
numpy.random.randint(0, 10, size=10)
. The method you show is needlessly inefficient.– Mark Dickinson
Jul 6 '18 at 6:44
add a comment |
Best way is to use import Random function
import random
print(random.sample(range(10), 10))
or without any library import:
n={}
for i in range(10):
n[i]=i
for p in range(10):
print(n.popitem()[1])
here the popitems removes and returns an arbitrary value from the dictionary n
.
add a comment |
Best way is to use import Random function
import random
print(random.sample(range(10), 10))
or without any library import:
n={}
for i in range(10):
n[i]=i
for p in range(10):
print(n.popitem()[1])
here the popitems removes and returns an arbitrary value from the dictionary n
.
add a comment |
Best way is to use import Random function
import random
print(random.sample(range(10), 10))
or without any library import:
n={}
for i in range(10):
n[i]=i
for p in range(10):
print(n.popitem()[1])
here the popitems removes and returns an arbitrary value from the dictionary n
.
Best way is to use import Random function
import random
print(random.sample(range(10), 10))
or without any library import:
n={}
for i in range(10):
n[i]=i
for p in range(10):
print(n.popitem()[1])
here the popitems removes and returns an arbitrary value from the dictionary n
.
edited Jun 3 '17 at 1:41
MSeifert
77.3k18148185
77.3k18148185
answered May 15 '17 at 18:29
S T MohammedS T Mohammed
80126
80126
add a comment |
add a comment |
You can try this:
import numpy as np
print ( np.random.uniform(low=0, high=10, size=(15,)) ).astype(int)
>>> [8 3 6 9 1 0 3 6 3 3 1 2 4 0 4]
Notes:
1.> np.random.uniform generates uniformly distributed numbers over the half-open interval [low, high).
2.> astype(int) casts the numpy array to int data type.
3.> I have chosen size = (15,). This will give you a numpy array of length = 15.
More information on numpy.random.uniform
More information on numpy.ndarray.astype
add a comment |
You can try this:
import numpy as np
print ( np.random.uniform(low=0, high=10, size=(15,)) ).astype(int)
>>> [8 3 6 9 1 0 3 6 3 3 1 2 4 0 4]
Notes:
1.> np.random.uniform generates uniformly distributed numbers over the half-open interval [low, high).
2.> astype(int) casts the numpy array to int data type.
3.> I have chosen size = (15,). This will give you a numpy array of length = 15.
More information on numpy.random.uniform
More information on numpy.ndarray.astype
add a comment |
You can try this:
import numpy as np
print ( np.random.uniform(low=0, high=10, size=(15,)) ).astype(int)
>>> [8 3 6 9 1 0 3 6 3 3 1 2 4 0 4]
Notes:
1.> np.random.uniform generates uniformly distributed numbers over the half-open interval [low, high).
2.> astype(int) casts the numpy array to int data type.
3.> I have chosen size = (15,). This will give you a numpy array of length = 15.
More information on numpy.random.uniform
More information on numpy.ndarray.astype
You can try this:
import numpy as np
print ( np.random.uniform(low=0, high=10, size=(15,)) ).astype(int)
>>> [8 3 6 9 1 0 3 6 3 3 1 2 4 0 4]
Notes:
1.> np.random.uniform generates uniformly distributed numbers over the half-open interval [low, high).
2.> astype(int) casts the numpy array to int data type.
3.> I have chosen size = (15,). This will give you a numpy array of length = 15.
More information on numpy.random.uniform
More information on numpy.ndarray.astype
edited Dec 25 '18 at 23:14
answered Nov 28 '18 at 7:56


Siddharth SatpathySiddharth Satpathy
5841717
5841717
add a comment |
add a comment |
I used variable to control the range
from random import randint
numberStartRange = 1
numberEndRange = 9
randomNumber = randint(numberStartRange, numberEndRange)
print(randomNumber)
I used the print function to see the results. You can comment is out if you do not need this.
add a comment |
I used variable to control the range
from random import randint
numberStartRange = 1
numberEndRange = 9
randomNumber = randint(numberStartRange, numberEndRange)
print(randomNumber)
I used the print function to see the results. You can comment is out if you do not need this.
add a comment |
I used variable to control the range
from random import randint
numberStartRange = 1
numberEndRange = 9
randomNumber = randint(numberStartRange, numberEndRange)
print(randomNumber)
I used the print function to see the results. You can comment is out if you do not need this.
I used variable to control the range
from random import randint
numberStartRange = 1
numberEndRange = 9
randomNumber = randint(numberStartRange, numberEndRange)
print(randomNumber)
I used the print function to see the results. You can comment is out if you do not need this.
edited Oct 24 '17 at 21:12
answered Sep 19 '17 at 11:30


Amir Md AmiruzzamanAmir Md Amiruzzaman
1,0931718
1,0931718
add a comment |
add a comment |
This is more of a mathematical approach but it works 100% of the time:
Let's say you want to use random.random()
function to generate a number between a
and b
. To achieve this, just do the following:
num = (b-a)*random.random() + a;
Of course, you can generate more numbers.
add a comment |
This is more of a mathematical approach but it works 100% of the time:
Let's say you want to use random.random()
function to generate a number between a
and b
. To achieve this, just do the following:
num = (b-a)*random.random() + a;
Of course, you can generate more numbers.
add a comment |
This is more of a mathematical approach but it works 100% of the time:
Let's say you want to use random.random()
function to generate a number between a
and b
. To achieve this, just do the following:
num = (b-a)*random.random() + a;
Of course, you can generate more numbers.
This is more of a mathematical approach but it works 100% of the time:
Let's say you want to use random.random()
function to generate a number between a
and b
. To achieve this, just do the following:
num = (b-a)*random.random() + a;
Of course, you can generate more numbers.
answered Nov 30 '18 at 18:27


Orestis ZekaiOrestis Zekai
508
508
add a comment |
add a comment |
For the example that you have given (a random integer between 0 and 9), the cleanest solution is:
from random import randrange
randrange(10)
randrange returns a single number between specified range.
– Shital Shah
Jan 2 at 1:43
@ShitalShah That is exactly what the question wants. Run the accepted answer and realize that it does the same thing.
– Utku
Jan 2 at 2:06
@ShitalShah If you want all numbers between 0 and 9, you userange(10)
.
– Utku
Jan 2 at 2:07
add a comment |
For the example that you have given (a random integer between 0 and 9), the cleanest solution is:
from random import randrange
randrange(10)
randrange returns a single number between specified range.
– Shital Shah
Jan 2 at 1:43
@ShitalShah That is exactly what the question wants. Run the accepted answer and realize that it does the same thing.
– Utku
Jan 2 at 2:06
@ShitalShah If you want all numbers between 0 and 9, you userange(10)
.
– Utku
Jan 2 at 2:07
add a comment |
For the example that you have given (a random integer between 0 and 9), the cleanest solution is:
from random import randrange
randrange(10)
For the example that you have given (a random integer between 0 and 9), the cleanest solution is:
from random import randrange
randrange(10)
edited Jan 2 at 2:08
answered Oct 8 '18 at 14:38
UtkuUtku
884929
884929
randrange returns a single number between specified range.
– Shital Shah
Jan 2 at 1:43
@ShitalShah That is exactly what the question wants. Run the accepted answer and realize that it does the same thing.
– Utku
Jan 2 at 2:06
@ShitalShah If you want all numbers between 0 and 9, you userange(10)
.
– Utku
Jan 2 at 2:07
add a comment |
randrange returns a single number between specified range.
– Shital Shah
Jan 2 at 1:43
@ShitalShah That is exactly what the question wants. Run the accepted answer and realize that it does the same thing.
– Utku
Jan 2 at 2:06
@ShitalShah If you want all numbers between 0 and 9, you userange(10)
.
– Utku
Jan 2 at 2:07
randrange returns a single number between specified range.
– Shital Shah
Jan 2 at 1:43
randrange returns a single number between specified range.
– Shital Shah
Jan 2 at 1:43
@ShitalShah That is exactly what the question wants. Run the accepted answer and realize that it does the same thing.
– Utku
Jan 2 at 2:06
@ShitalShah That is exactly what the question wants. Run the accepted answer and realize that it does the same thing.
– Utku
Jan 2 at 2:06
@ShitalShah If you want all numbers between 0 and 9, you use
range(10)
.– Utku
Jan 2 at 2:07
@ShitalShah If you want all numbers between 0 and 9, you use
range(10)
.– Utku
Jan 2 at 2:07
add a comment |
Try This,
import numpy as np
X = np.random.randint(0, 99, size=1000) # 1k random integer
add a comment |
Try This,
import numpy as np
X = np.random.randint(0, 99, size=1000) # 1k random integer
add a comment |
Try This,
import numpy as np
X = np.random.randint(0, 99, size=1000) # 1k random integer
Try This,
import numpy as np
X = np.random.randint(0, 99, size=1000) # 1k random integer
answered Dec 19 '18 at 10:38


Rakesh ChaudhariRakesh Chaudhari
1,06711420
1,06711420
add a comment |
add a comment |
From the documentation page for the random module:
Warning: The pseudo-random generators of this module should not be
used for security purposes. Use os.urandom() or SystemRandom if you
require a cryptographically secure pseudo-random number generator.
random.SystemRandom, which was introduced in Python 2.4, is considered cryptographically secure. It is still available in Python 3.7.1 which is current at time of writing.
>>> import string
>>> string.digits
'0123456789'
>>> import random
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'1'
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'5'
Instead of string.digits
, range
could be used per some of the other answers along perhaps with a comprehension. Mix and match according to your needs.
add a comment |
From the documentation page for the random module:
Warning: The pseudo-random generators of this module should not be
used for security purposes. Use os.urandom() or SystemRandom if you
require a cryptographically secure pseudo-random number generator.
random.SystemRandom, which was introduced in Python 2.4, is considered cryptographically secure. It is still available in Python 3.7.1 which is current at time of writing.
>>> import string
>>> string.digits
'0123456789'
>>> import random
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'1'
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'5'
Instead of string.digits
, range
could be used per some of the other answers along perhaps with a comprehension. Mix and match according to your needs.
add a comment |
From the documentation page for the random module:
Warning: The pseudo-random generators of this module should not be
used for security purposes. Use os.urandom() or SystemRandom if you
require a cryptographically secure pseudo-random number generator.
random.SystemRandom, which was introduced in Python 2.4, is considered cryptographically secure. It is still available in Python 3.7.1 which is current at time of writing.
>>> import string
>>> string.digits
'0123456789'
>>> import random
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'1'
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'5'
Instead of string.digits
, range
could be used per some of the other answers along perhaps with a comprehension. Mix and match according to your needs.
From the documentation page for the random module:
Warning: The pseudo-random generators of this module should not be
used for security purposes. Use os.urandom() or SystemRandom if you
require a cryptographically secure pseudo-random number generator.
random.SystemRandom, which was introduced in Python 2.4, is considered cryptographically secure. It is still available in Python 3.7.1 which is current at time of writing.
>>> import string
>>> string.digits
'0123456789'
>>> import random
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'1'
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'5'
Instead of string.digits
, range
could be used per some of the other answers along perhaps with a comprehension. Mix and match according to your needs.
edited Feb 7 at 16:49
answered Dec 11 '18 at 22:49


Richard RiehleRichard Riehle
1036
1036
add a comment |
add a comment |
I had better luck with this for Python 3.6
str_Key = ""
str_RandomKey = ""
for int_I in range(128):
str_Key = random.choice('0123456789')
str_RandomKey = str_RandomKey + str_Key
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.
add a comment |
I had better luck with this for Python 3.6
str_Key = ""
str_RandomKey = ""
for int_I in range(128):
str_Key = random.choice('0123456789')
str_RandomKey = str_RandomKey + str_Key
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.
add a comment |
I had better luck with this for Python 3.6
str_Key = ""
str_RandomKey = ""
for int_I in range(128):
str_Key = random.choice('0123456789')
str_RandomKey = str_RandomKey + str_Key
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.
I had better luck with this for Python 3.6
str_Key = ""
str_RandomKey = ""
for int_I in range(128):
str_Key = random.choice('0123456789')
str_RandomKey = str_RandomKey + str_Key
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.
answered Jul 20 '17 at 23:43


M T HeadM T Head
511411
511411
add a comment |
add a comment |
protected by Aniket Thakur Sep 9 '15 at 8:43
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?