About 325,000 results
Open links in new tab
  1. python - Generate random integers between 0 and 9 - Stack …

    2 If we look at their source implementations, random.randrange() (and random.randint() because it is the former's syntactic sugar) use a while-loop to generate a pseudo-random number via _randbelow method while random.choices() calls random() once and uses it to index the population. So if a lot of pseudo-random numbers need to be generated ...

  2. python - Random int without importing 'random' - Stack Overflow

    Apr 9, 2014 · This will generate a list of uniformly distributed pseudo-random numbers between 0 and 1. Like the random number generators in random and numpy the sequence is fully deterministic for a given seed. Histogram of the function's output. This algorithm is in no way cryptographically safe but it should be sufficient to answer your question.

  3. Python random function - Stack Overflow

    Feb 21, 2013 · import random will import the random module whereas from random import random will specifically import the random function from the module. So you will be able to do one of the following: import random a = random.random() b = random.randint(1, 5) # you can call any function from the random module using random.<function> or...

  4. python - How to generate a random number with a specific …

    Jul 4, 2021 · import random num = random.randrange(1, 10**3) # using format num_with_zeros = '{:03}'.format(num) # using string's zfill num_with_zeros = str(num).zfill(3) Alternatively if you don't want to save the random number as an int you can just do it as a oneliner: '{:03}'.format(random.randrange(1, 10**3)) python 3.6+ only oneliner:

  5. python - How to get a random number between a float range

    Oct 24, 2023 · import random random.uniform(a, b) # range [a, b) or [a, b] depending on floating-point rounding Python provides other distributions if you need. If you have numpy imported already, you can used its equivalent: import numpy as np np.random.uniform(a, b) # range [a, b)

  6. python - How can I randomly select (choose) an item from a list …

    NumPy solution: numpy.random.choice. For this question, it works the same as the accepted answer (import random; random.choice()), but I added it because the programmer may have imported NumPy already (like me)

  7. Python: Random numbers into a list - Stack Overflow

    import random my_randoms = random.sample(range(100), 10) That generates numbers in the (inclusive) range from 0 to 99. If you want 1 to 100, you could use this (thanks to @martineau for pointing out my convoluted solution):

  8. Python Random Function without using random module

    Feb 25, 2015 · If getrandbits(k) returns k random bits then randint(a, b) should work as is (no skew due to modulo, etc). To test the quality of getrandbits(k), dieharder utility could be used: $ python3 random-from-time.py | dieharder -a -g 200 where random-from-time.py generates infinite (random) binary stream:

  9. Python random numbers multiple times - Stack Overflow

    Apr 18, 2015 · I would recommend using a loop that simply adds to an existing string: import random from random import randint list="" number1=input("Put in the first number: ") number2=input("Put in the second number: ") total=input("Put in how many numbers generated: ") times_run=0 while times_run<=total: gen1=random.randint(number1, number2) list=str(list)+str(gen1)+" " times_run+=1 print list

  10. Python "import random" Error - Stack Overflow

    import random number = random.randint(1,10000) Python gives me this error: File "C\Users\name\Documents\Python\random.py", line 5, in (module) print random.random() TypeError: 'module' object is not callable Every time I try to run it. Me no understand. Any help would be much appreciated! EDIT: The two lines of code I'm trying to run:

Refresh