Generate random integers between 0 and 9












1067















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










share|improve this question





























    1067















    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










    share|improve this question



























      1067












      1067








      1067


      133






      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










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 18 '18 at 8:08









      K48

      5,62594096




      5,62594096










      asked Oct 22 '10 at 12:48









      aneuryzmaneuryzm

      21k87238448




      21k87238448
























          21 Answers
          21






          active

          oldest

          votes


















          1713














          Try:



          from random import randint
          print(randint(0, 9))


          More info: https://docs.python.org/3/library/random.html#random.randint






          share|improve this answer





















          • 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



















          311














          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






          share|improve this answer

































            101














            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)





            share|improve this answer

































              59














              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.






              share|improve this answer





















              • 3





                10 or 9? I'm getting only 9.

                – Sigur
                May 11 '17 at 13:52



















              42














              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.






              share|improve this answer





















              • 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



















              24














              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]





              share|improve this answer





















              • 3





                nums = range(10)

                – Kamejoin
                Dec 14 '17 at 20:10



















              17














              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]])





              share|improve this answer



















              • 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





















              15














              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





              share|improve this answer


























              • 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





















              10














              if you want to use numpy then use the following:



              import numpy as np
              print(np.random.randint(0,10))





              share|improve this answer





















              • 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





















              10














              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.






              share|improve this answer

































                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]





                share|improve this answer































                  6














                  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





                  share|improve this answer































                    6














                    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]





                    share|improve this answer





















                    • 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



















                    5














                    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.






                    share|improve this answer

































                      3














                      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






                      share|improve this answer

































                        2














                        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.






                        share|improve this answer

































                          2














                          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.






                          share|improve this answer































                            2














                            For the example that you have given (a random integer between 0 and 9), the cleanest solution is:



                            from random import randrange

                            randrange(10)





                            share|improve this answer


























                            • 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 use range(10).

                              – Utku
                              Jan 2 at 2:07



















                            1














                            Try This,



                            import numpy as np

                            X = np.random.randint(0, 99, size=1000) # 1k random integer





                            share|improve this answer































                              1














                              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.






                              share|improve this answer

































                                -1














                                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.






                                share|improve this answer






















                                  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









                                  1713














                                  Try:



                                  from random import randint
                                  print(randint(0, 9))


                                  More info: https://docs.python.org/3/library/random.html#random.randint






                                  share|improve this answer





















                                  • 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
















                                  1713














                                  Try:



                                  from random import randint
                                  print(randint(0, 9))


                                  More info: https://docs.python.org/3/library/random.html#random.randint






                                  share|improve this answer





















                                  • 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














                                  1713












                                  1713








                                  1713







                                  Try:



                                  from random import randint
                                  print(randint(0, 9))


                                  More info: https://docs.python.org/3/library/random.html#random.randint






                                  share|improve this answer















                                  Try:



                                  from random import randint
                                  print(randint(0, 9))


                                  More info: https://docs.python.org/3/library/random.html#random.randint







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  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 the secrets module for better random numbers. Reference: docs.python.org/3/library/random.html

                                    – K48
                                    Oct 18 '18 at 8:08














                                  • 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








                                  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













                                  311














                                  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






                                  share|improve this answer






























                                    311














                                    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






                                    share|improve this answer




























                                      311












                                      311








                                      311







                                      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






                                      share|improve this answer















                                      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







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jun 30 '15 at 14:51









                                      FlipperPA

                                      7,23622145




                                      7,23622145










                                      answered May 4 '13 at 17:13









                                      JMSamudioJMSamudio

                                      3,307178




                                      3,307178























                                          101














                                          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)





                                          share|improve this answer






























                                            101














                                            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)





                                            share|improve this answer




























                                              101












                                              101








                                              101







                                              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)





                                              share|improve this answer















                                              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)






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Oct 22 '10 at 12:55

























                                              answered Oct 22 '10 at 12:49









                                              Andrew HareAndrew Hare

                                              279k54581602




                                              279k54581602























                                                  59














                                                  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.






                                                  share|improve this answer





















                                                  • 3





                                                    10 or 9? I'm getting only 9.

                                                    – Sigur
                                                    May 11 '17 at 13:52
















                                                  59














                                                  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.






                                                  share|improve this answer





















                                                  • 3





                                                    10 or 9? I'm getting only 9.

                                                    – Sigur
                                                    May 11 '17 at 13:52














                                                  59












                                                  59








                                                  59







                                                  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.






                                                  share|improve this answer















                                                  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.







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  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














                                                  • 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











                                                  42














                                                  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.






                                                  share|improve this answer





















                                                  • 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
















                                                  42














                                                  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.






                                                  share|improve this answer





















                                                  • 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














                                                  42












                                                  42








                                                  42







                                                  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.






                                                  share|improve this answer















                                                  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.







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  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














                                                  • 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











                                                  24














                                                  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]





                                                  share|improve this answer





















                                                  • 3





                                                    nums = range(10)

                                                    – Kamejoin
                                                    Dec 14 '17 at 20:10
















                                                  24














                                                  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]





                                                  share|improve this answer





















                                                  • 3





                                                    nums = range(10)

                                                    – Kamejoin
                                                    Dec 14 '17 at 20:10














                                                  24












                                                  24








                                                  24







                                                  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]





                                                  share|improve this answer















                                                  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]






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  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














                                                  • 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











                                                  17














                                                  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]])





                                                  share|improve this answer



















                                                  • 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


















                                                  17














                                                  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]])





                                                  share|improve this answer



















                                                  • 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
















                                                  17












                                                  17








                                                  17







                                                  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]])





                                                  share|improve this answer













                                                  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]])






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  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
















                                                  • 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













                                                  15














                                                  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





                                                  share|improve this answer


























                                                  • 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


















                                                  15














                                                  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





                                                  share|improve this answer


























                                                  • 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
















                                                  15












                                                  15








                                                  15







                                                  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





                                                  share|improve this answer















                                                  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






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  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 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





















                                                  • 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



















                                                  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













                                                  10














                                                  if you want to use numpy then use the following:



                                                  import numpy as np
                                                  print(np.random.randint(0,10))





                                                  share|improve this answer





















                                                  • 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


















                                                  10














                                                  if you want to use numpy then use the following:



                                                  import numpy as np
                                                  print(np.random.randint(0,10))





                                                  share|improve this answer





















                                                  • 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
















                                                  10












                                                  10








                                                  10







                                                  if you want to use numpy then use the following:



                                                  import numpy as np
                                                  print(np.random.randint(0,10))





                                                  share|improve this answer















                                                  if you want to use numpy then use the following:



                                                  import numpy as np
                                                  print(np.random.randint(0,10))






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  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
















                                                  • 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













                                                  10














                                                  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.






                                                  share|improve this answer






























                                                    10














                                                    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.






                                                    share|improve this answer




























                                                      10












                                                      10








                                                      10







                                                      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.






                                                      share|improve this answer















                                                      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.







                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      edited Nov 16 '17 at 4:37

























                                                      answered Sep 22 '17 at 4:29









                                                      pylangpylang

                                                      14.1k24458




                                                      14.1k24458























                                                          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]





                                                          share|improve this answer




























                                                            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]





                                                            share|improve this answer


























                                                              8












                                                              8








                                                              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]





                                                              share|improve this answer













                                                              >>> 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]






                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Aug 10 '17 at 15:48









                                                              John Lawrence AspdenJohn Lawrence Aspden

                                                              11.1k105190




                                                              11.1k105190























                                                                  6














                                                                  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





                                                                  share|improve this answer




























                                                                    6














                                                                    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





                                                                    share|improve this answer


























                                                                      6












                                                                      6








                                                                      6







                                                                      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





                                                                      share|improve this answer













                                                                      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






                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered May 14 '17 at 16:46









                                                                      prashanthprashanth

                                                                      1,7061227




                                                                      1,7061227























                                                                          6














                                                                          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]





                                                                          share|improve this answer





















                                                                          • 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
















                                                                          6














                                                                          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]





                                                                          share|improve this answer





















                                                                          • 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














                                                                          6












                                                                          6








                                                                          6







                                                                          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]





                                                                          share|improve this answer















                                                                          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]






                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          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 with numpy.random.randint(0, 10, size=10). The method you show is needlessly inefficient.

                                                                            – Mark Dickinson
                                                                            Jul 6 '18 at 6:44














                                                                          • 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








                                                                          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











                                                                          5














                                                                          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.






                                                                          share|improve this answer






























                                                                            5














                                                                            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.






                                                                            share|improve this answer




























                                                                              5












                                                                              5








                                                                              5







                                                                              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.






                                                                              share|improve this answer















                                                                              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.







                                                                              share|improve this answer














                                                                              share|improve this answer



                                                                              share|improve this answer








                                                                              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























                                                                                  3














                                                                                  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






                                                                                  share|improve this answer






























                                                                                    3














                                                                                    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






                                                                                    share|improve this answer




























                                                                                      3












                                                                                      3








                                                                                      3







                                                                                      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






                                                                                      share|improve this answer















                                                                                      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







                                                                                      share|improve this answer














                                                                                      share|improve this answer



                                                                                      share|improve this answer








                                                                                      edited Dec 25 '18 at 23:14

























                                                                                      answered Nov 28 '18 at 7:56









                                                                                      Siddharth SatpathySiddharth Satpathy

                                                                                      5841717




                                                                                      5841717























                                                                                          2














                                                                                          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.






                                                                                          share|improve this answer






























                                                                                            2














                                                                                            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.






                                                                                            share|improve this answer




























                                                                                              2












                                                                                              2








                                                                                              2







                                                                                              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.






                                                                                              share|improve this answer















                                                                                              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.







                                                                                              share|improve this answer














                                                                                              share|improve this answer



                                                                                              share|improve this answer








                                                                                              edited Oct 24 '17 at 21:12

























                                                                                              answered Sep 19 '17 at 11:30









                                                                                              Amir Md AmiruzzamanAmir Md Amiruzzaman

                                                                                              1,0931718




                                                                                              1,0931718























                                                                                                  2














                                                                                                  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.






                                                                                                  share|improve this answer




























                                                                                                    2














                                                                                                    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.






                                                                                                    share|improve this answer


























                                                                                                      2












                                                                                                      2








                                                                                                      2







                                                                                                      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.






                                                                                                      share|improve this answer













                                                                                                      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.







                                                                                                      share|improve this answer












                                                                                                      share|improve this answer



                                                                                                      share|improve this answer










                                                                                                      answered Nov 30 '18 at 18:27









                                                                                                      Orestis ZekaiOrestis Zekai

                                                                                                      508




                                                                                                      508























                                                                                                          2














                                                                                                          For the example that you have given (a random integer between 0 and 9), the cleanest solution is:



                                                                                                          from random import randrange

                                                                                                          randrange(10)





                                                                                                          share|improve this answer


























                                                                                                          • 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 use range(10).

                                                                                                            – Utku
                                                                                                            Jan 2 at 2:07
















                                                                                                          2














                                                                                                          For the example that you have given (a random integer between 0 and 9), the cleanest solution is:



                                                                                                          from random import randrange

                                                                                                          randrange(10)





                                                                                                          share|improve this answer


























                                                                                                          • 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 use range(10).

                                                                                                            – Utku
                                                                                                            Jan 2 at 2:07














                                                                                                          2












                                                                                                          2








                                                                                                          2







                                                                                                          For the example that you have given (a random integer between 0 and 9), the cleanest solution is:



                                                                                                          from random import randrange

                                                                                                          randrange(10)





                                                                                                          share|improve this answer















                                                                                                          For the example that you have given (a random integer between 0 and 9), the cleanest solution is:



                                                                                                          from random import randrange

                                                                                                          randrange(10)






                                                                                                          share|improve this answer














                                                                                                          share|improve this answer



                                                                                                          share|improve this answer








                                                                                                          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 use range(10).

                                                                                                            – Utku
                                                                                                            Jan 2 at 2:07



















                                                                                                          • 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 use range(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











                                                                                                          1














                                                                                                          Try This,



                                                                                                          import numpy as np

                                                                                                          X = np.random.randint(0, 99, size=1000) # 1k random integer





                                                                                                          share|improve this answer




























                                                                                                            1














                                                                                                            Try This,



                                                                                                            import numpy as np

                                                                                                            X = np.random.randint(0, 99, size=1000) # 1k random integer





                                                                                                            share|improve this answer


























                                                                                                              1












                                                                                                              1








                                                                                                              1







                                                                                                              Try This,



                                                                                                              import numpy as np

                                                                                                              X = np.random.randint(0, 99, size=1000) # 1k random integer





                                                                                                              share|improve this answer













                                                                                                              Try This,



                                                                                                              import numpy as np

                                                                                                              X = np.random.randint(0, 99, size=1000) # 1k random integer






                                                                                                              share|improve this answer












                                                                                                              share|improve this answer



                                                                                                              share|improve this answer










                                                                                                              answered Dec 19 '18 at 10:38









                                                                                                              Rakesh ChaudhariRakesh Chaudhari

                                                                                                              1,06711420




                                                                                                              1,06711420























                                                                                                                  1














                                                                                                                  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.






                                                                                                                  share|improve this answer






























                                                                                                                    1














                                                                                                                    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.






                                                                                                                    share|improve this answer




























                                                                                                                      1












                                                                                                                      1








                                                                                                                      1







                                                                                                                      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.






                                                                                                                      share|improve this answer















                                                                                                                      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.







                                                                                                                      share|improve this answer














                                                                                                                      share|improve this answer



                                                                                                                      share|improve this answer








                                                                                                                      edited Feb 7 at 16:49

























                                                                                                                      answered Dec 11 '18 at 22:49









                                                                                                                      Richard RiehleRichard Riehle

                                                                                                                      1036




                                                                                                                      1036























                                                                                                                          -1














                                                                                                                          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.






                                                                                                                          share|improve this answer




























                                                                                                                            -1














                                                                                                                            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.






                                                                                                                            share|improve this answer


























                                                                                                                              -1












                                                                                                                              -1








                                                                                                                              -1







                                                                                                                              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.






                                                                                                                              share|improve this answer













                                                                                                                              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.







                                                                                                                              share|improve this answer












                                                                                                                              share|improve this answer



                                                                                                                              share|improve this answer










                                                                                                                              answered Jul 20 '17 at 23:43









                                                                                                                              M T HeadM T Head

                                                                                                                              511411




                                                                                                                              511411

















                                                                                                                                  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?



                                                                                                                                  Popular posts from this blog

                                                                                                                                  MongoDB - Not Authorized To Execute Command

                                                                                                                                  How to fix TextFormField cause rebuild widget in Flutter

                                                                                                                                  in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith