Table of Contents
Which Python Function Returns a Random Float in the Range [0.0, 1.0)?
Learn the correct function in Python’s random module to generate a random float in the half-open interval [0.0, 1.0). This guide explains why random.random() is the standard choice and clarifies the purpose of other related functions.
Question
Which call returns a random float in the half-open interval [0.0, 1.0)?
A. random.random()
B. random.randint(0, 1)
C. random.uniform(0, 1, 10)
D. random.range(0, 1)
Answer
A. random.random()
Explanation
Produces a float in [0.0, 1.0).
The function random.random() returns a pseudo-random floating-point number in the half-open interval [0.0, 1.0).
This function is specifically designed for this purpose. It takes no arguments and returns a float x such that 0.0 <= x < 1.0. The notation [0.0, 1.0) signifies that the lower bound, 0.0, is included in the possible results, while the upper bound, 1.0, is excluded. This function is a core component of the random module and is often used as a basis for generating random numbers in other ranges.
The other options are incorrect for the following reasons:
B. random.randint(0, 1): This function returns a random integer, not a float. It will only ever produce the value 0 or 1.
C. random.uniform(0, 1, 10): This call is invalid because the random.uniform() function only accepts two arguments (a, b). Even with correct usage, random.uniform(0, 1) would return a float in the inclusive range [0.0, 1.0], which does not match the required half-open interval.
D. random.range(0, 1): The random module does not contain a function named range. This is likely a confusion with the built-in range() function or random.randrange(), both of which work with integers.
Python Basics: Learn, Apply & Build Programs certification exam assessment practice question and answer (Q&A) dump including multiple choice questions (MCQ) and objective type questions, with detail explanation and reference available free, helpful to pass the Python Basics: Learn, Apply & Build Programs exam and earn Python Basics: Learn, Apply & Build Programs certificate.