Try random.randrange:
from random import randrange
print(randrange(10))
Answer from kovshenin on Stack OverflowPython Rand+Range Question
python - Generate random integers between 0 and 9 - Stack Overflow
How to generate a range of random numbers in python without repetition - Stack Overflow
python - Generating random numbers in a specific range - Stack Overflow
Videos
So Im working on a random number generator, and ive noticed that for some reason, Range will always generate the first number in the range, but never the last. What is the reason for this? Is it the whole thing of "Computers start with 0, not 1" thing?
Code for example:
import random
print("Test Result: ")
print(random.randrange(1,100))The random module has a sample function, which is predicated on producing unique elements. It is used as in the following example:
random_list = random.sample(xrange(10000000), 60)
# generates a list of 60 random numbers in the
# range 0 to 10000000 without repetition
But, do be aware that it will throw an exception if the length of the list is greater than the size of the population, e.g.
random_list = random.sample(xrange(5), 60 # you will get a ValueError here
You can do like this:
input = set()
for i in range(MAX):
input.add(random.randrange(x,y))
print input
with in the range of x and y it will select some random values. W/o repetition means you can use set. so i am adding those values to set.
just make a try this one.
Perhaps the easiest way to solve your problem is to think in terms of integers, not floating-point numbers.
You basically want possible random numbers like 0.01, 0.02, 0.03, ..., 0.09, 0.10.
First you generate an integer between 1 to 10 inclusive, then you divide by 100.0 to get a floating-point number.
Here is the code:
x = random.randint(1, 10)
y = x / 100.0
Documentation: random.randint(a, b)
radius = round(random.uniform(0.005, 0.10499999999999), 2)
print(radius)
you also need to extend the range of the sampled values to make sure that the probability of selecting the edge numbers 0.01 and 0.1 is the same as that of any other number in the desired range. otherwise, they will be sampled with half of probability of other numbers. I assume that round function rounds up i.e. 0.105 would end up as 0.11 not 0.10.
There's an optional thing called step which I am trying to understand. "random.randrange(start,stop,step)
Word "increment" is not familiar for me so please explain it as understandable as possible. Thank you
The docs on randrange say:
random.randrange([start], stop[, step])Return a randomly selected element from
range(start, stop, step). This is equivalent tochoice(range(start, stop, step)), but doesnโt actually build a range object.
And range(start, stop) returns [start, start+step, ..., stop-1], not [start, start+step, ..., stop]. As for why... zero-based counting rules and range(n) should return n elements, I suppose. Most useful for getting a random index, I suppose.
While randint is documented as:
random.randint(a, b)Return a random integer N such that
a <= N <= b. Alias forrandrange(a, b+1)
So randint is for when you have the maximum and minimum value for the random number you want.
https://github.com/python/cpython/blob/.../Lib/random.py#L218
def randint(self, a, b):
"""Return random integer in range [a, b], including both end points.
"""
return self.randrange(a, b+1)