Discussions

python - How to get a random number between a float range? - Stack Overflow
random.randrange(start, stop) only takes integer arguments. So how would I get a random number between two float values? More on stackoverflow.com
🌐 stackoverflow.com
list - Python generate n evenly spaced float numbers between two values - Stack Overflow
I would like to generate n float numbers between two numbers. I am giving an example of what i am expecting. num_list = [1.2, 2.9] I am expecting a output something like this below but not necess... More on stackoverflow.com
🌐 stackoverflow.com
python - How to get all integers between two float values? - Stack Overflow
I want to get all the integers which are between two float values. Example: I have an interval (2.4 , 5.6). As an output I want to have (3, 4, 5). ... Sign up to request clarification or add additional context in comments. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Does there exist a number ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to check if a float value is within a certain range and has a given number of decimal digits? - Stack Overflow
How to check if a float value is within a range (0.50,150.00) and has 2 decimal digits? For example, 15.22366 should be false (too many decimal digits). But 15.22 should be true. I tried somethin... More on stackoverflow.com
🌐 stackoverflow.com
🌐
CodeSpeedy
codespeedy.com › home › how to generate random floating numbers in python
How to generate random floating numbers in Python - CodeSpeedy
February 17, 2022 - #importing required libraries import random #getting random float number between two float numbers using uniform method ran_flo=random.uniform(6.66,15.99) ran_flo=round(ran_flo,3) print(ran_flo)
🌐
TechBeamers
techbeamers.com › python-float-range
Generate Floating Point Range in Python - TechBeamers
November 30, 2025 - Python range can only generate a set of integer numbers from a given band. Neither does it allow a float type parameter nor it can produce a float range of numbers. It accepts one, two, or three parameters (start/stop/step).
🌐
Python documentation
docs.python.org › 3 › tutorial › floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations — Python 3.14.7 documentation
Stop at any finite number of bits, and you get an approximation. On most machines today, floats are approximated using a binary fraction with the numerator using the first 53 bits starting with the most significant bit and with the denominator as a power of two.
🌐
Mimo
mimo.org › glossary › python › float
Python Floats: Coding Essentials | Learn Now
A float in Python is created by including a decimal point in a number. ... Dividing two numbers results in a float, even if both numbers are integers.
Find elsewhere
🌐
PYnative
pynative.com › home › python › python range of float numbers
Python range of float numbers
April 13, 2021 - Use Python's numpy arange() and linspace() functions to generate a range of float numbers. Use decimal numbers as start, stop, and step value
🌐
Sololearn
sololearn.com › en › Discuss › 2983436 › how-to-test-if-a-float-number-exists-between-two-int-numbers
https://www.sololearn.com/en/Discuss/2983436/how-t...
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
The FinAnalytics
thefinanalytics.com › post › understanding-python-floats-operations-casting-and-best-practices
Understanding Python Floats: Operations, Casting, and Best Practices
June 1, 2025 - Two computations that should mathematically be the same might end up with slight differences in the last decimal places. Instead, it's recommended to check if they are close enough. Python’s math.isclose() function (or writing a small tolerance check) is useful for this – it considers two floats “close” if the difference between them is within a specified tolerance.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to generate a python list of floats between two numbers
5 Best Ways to Generate a Python List of Floats Between Two Numbers - Be on the Right Side of Change
February 24, 2024 - This code utilizes the arange() function from NumPy, a powerful mathematical library in Python, to directly create an array of floats. The resulting numpy.ndarray can then be converted to a list using tolist(). This method is easy to write and read, but it adds a dependency on NumPy, which may not be ideal for all projects. A generator function can be written to yield floats in a desired range with a given step. This is an efficient way of creating float ranges because it generates numbers on-the-fly.
Top answer
1 of 5
34

Is that you are looking for?

def check(value):
    if 0.50 <= value <= 150 and round(value,2)==value:
        return True
    return False

Given your comment:

i input 15.22366 it is going to return true; that is why i specified the range; it should accept 15.22

Simply said, floating point values are imprecise. Many values don't have a precise representation. Say for example 1.40. It might be displayed "as it":

>>> f = 1.40
>>> print f
1.4

But this is an illusion. Python has rounded that value in order to nicely display it. The real value as referenced by the variable f is quite different:

>>> from decimal import Decimal
>>> Decimal(f)
Decimal('1.399999999999999911182158029987476766109466552734375')

According to your rule of having only 2 decimals, should f reference a valid value or not?

The easiest way to fix that issue is probably to use round(...,2) as I suggested in the code above. But this in only an heuristic -- only able to reject "largely wrong" values. See my point here:

>>> for v in [ 1.40,
...            1.405,
...            1.399999999999999911182158029987476766109466552734375,
...            1.39999999999999991118,
...            1.3999999999999991118]:
...     print check(v), v
...
True 1.4
False 1.405
True 1.4
True 1.4
False 1.4

Notice how the last few results might seems surprising at first. I hope my above explanations put some light on this.


As a final advice, for your needs as I guess them from your question, you should definitively consider using "decimal arithmetic". Python provides the decimal module for that purpose.

2 of 5
3

float is the wrong data type to use for your case, Use Decimal instead.

Check python docs for issues and limitations. To quote from there (I've generalised the text in Italics)

Floating-point numbers are represented in computer hardware as base 2 (binary) fractions.

no matter how many base 2 digits you’re willing to use, some decimal value (like 0.1) cannot be represented exactly as a base 2 fraction.

Stop at any finite number of bits, and you get an approximation

On a typical machine running Python, there are 53 bits of precision available for a Python float, so the value stored internally when you enter a decimal number is the binary fraction which is close to, but not exactly equal to it.

The documentation for the built-in round() function says that it rounds to the nearest value, rounding ties away from zero.

And finally, it recommends

If you’re in a situation where you care which way your decimal halfway-cases are rounded, you should consider using the decimal module.

And this will hold for your case as well, as you are looking for a precision of 2 digits after decimal points, which float just can't guarantee.


EDIT Note: The answer below corresponds to original question related to random float generation

Seeing that you need 2 digits of sure shot precision, I would suggest generating integer random numbers in range [50, 15000] and dividing them by 100 to convert them to float yourself.

import random
random.randint(50, 15000)/100.0
🌐
Snakify
snakify.org › integer and float numbers
Integer and float numbers - Learn Python 3 - Snakify
We already know the following operators which may be applied to numbers: +, -, * and **. The division operator / for integers gives a floating-point real number (an object of type float).
🌐
GeeksforGeeks
geeksforgeeks.org › python-generate-random-float-number
Generate Random Float Number in Python - GeeksforGeeks
May 8, 2025 - Here, we will see the various approaches for generating random numbers between 0 ans 1. Method 1: Here, we will use uniform() method which returns the random number between the two specified numbers ...
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-generate-random-float-numbers-in-pythonexample.html
How to Generate Random Float Numbers in Python? - ItSolutionstuff.com
October 30, 2023 - Today, I would like to show you python generate random float numbers. This tutorial will give you a simple example of python random float 2 decimal places. This example will help you random float between two numbers python.
🌐
Techie Delight
techiedelight.com › home › python › generate a random float in python
Generate a random float in Python | Techie Delight
July 7, 2026 - This post will discuss how to generate a random float between interval [0.0, 1.0) in Python. You can use the random.uniform(a, b) function to generate a pseudorandom floating-point number n such that a <= n <= b for a <= b.
🌐
Reddit
reddit.com › r/learnpython › float numbers with range () function
r/learnpython on Reddit: float numbers with range () function
April 19, 2022 -

Hi, I have a section of code which takes a score from the user, typecasts it into a float value and saves it under the variable name "score". Then I have a line of code which reads:

while score not in range(0, 101):

However, when I input a float, I get an invalid input error message from python. I think it's because I can't use the range() function with floats. Is there a way around this? If anyone knows how to solve this, then your help would be greatly appreciated. Thank you in advance.