Try random.randrange:

from random import randrange
print(randrange(10))
Answer from kovshenin on Stack Overflow
🌐
freeCodeCamp
forum.freecodecamp.org β€Ί javascript
Basic - Generating random number between 0-9 - JavaScript
September 26, 2020 - Tell us what’s happening: The ... be generated) So my initial solution was as follows: function randomWholeNum() { return Math.floor(Math.random()*9); } However, the requirement stated that I should be multiplying ...
Discussions

python - Generate random integers between 0 and 9 - Stack Overflow
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 More on stackoverflow.com
🌐 stackoverflow.com
Generating random whole numbers in JavaScript in a specific range - Stack Overflow
How can I generate random whole numbers between two specified variables in JavaScript, e.g. x = 4 and y = 8 would output any of 4, 5, 6, 7, 8? More on stackoverflow.com
🌐 stackoverflow.com
freeCodeCamp Challenge Guide: Generate Random Whole Numbers within a Range - Guide - The freeCodeCamp Forum
Generate Random Whole Numbers within a Range Hints Hint 1 randomRange should use both myMax and myMin, and return a random number in your range. You cannot pass the test if you are only re-using the function ourRandomRange inside your randomRange formula. You need to write your own formula ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
133
October 16, 2019
Whats your preferred way of generating a random number, e.g. between 1 and 100 ?
random.randint(0, 100) More on reddit.com
🌐 r/learnpython
41
6
January 25, 2022
🌐
freeCodeCamp
forum.freecodecamp.org β€Ί javascript
Basic JavaScript - Generate Random Whole Numbers with JavaScript - JavaScript - The freeCodeCamp Forum
April 18, 2023 - Tell us what’s happening: The instructions for this lesson ask you to use the Math.floor(Math.random()) * N to generate and return a random whole number between 0 and 9 When I return Math.floor(Math.random()*9) it is…
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί JavaScript β€Ί Reference β€Ί Global_Objects β€Ί Math β€Ί random
Math.random() - JavaScript - MDN Web Docs
The Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range β€” which you can then scale to your desired range. The implementation selects the initial seed to the random ...
🌐
Python documentation
docs.python.org β€Ί 3 β€Ί library β€Ί random.html
random β€” Generate pseudo-random numbers
3 weeks ago - Added in version 3.9. ... Return a randomly selected element from range(start, stop, step). This is roughly equivalent to choice(range(start, stop, step)) but supports arbitrarily large ranges and is optimized for common cases. The positional argument pattern matches the range() function. Keyword arguments should not be used because they can be interpreted in unexpected ways. For example randrange(start=100) is interpreted as randrange(0, 100, 1).
🌐
Number Generator
numbergenerator.org β€Ί randomnumbergenerator β€Ί 0-9
Random Number Between 0 And 9 - Number Generator
4 digit number generator 6 digit number generator Lottery Number Generator Β· Lets you pick a number between 0 and 9. Use the start/stop to achieve true randomness and add the luck factor.
Find elsewhere
🌐
CoreUI
coreui.io β€Ί blog β€Ί how-to-generate-a-random-number-in-javascript
Javascript Random - How to Generate a Random Number in JavaScript? Β· CoreUI
April 16, 2024 - This scales the random number to your desired range. Generating a whole number (integer) within a range involves a similar approach but includes steps to round off the decimal: const getRandomInteger = (min, max) => { min = Math.ceil(min) max = Math.floor(max) return Math.floor(Math.random() * (max - min)) + min } // Random integer between 5 and 9 const randomInteger = getRandomInteger(5, 10) console.log(randomInteger) // Random integer between 0 and 99 const randomInteger2 = getRandomInteger(0, 100) console.log(randomInteger2)
🌐
Programiz
programiz.com β€Ί python-programming β€Ί examples β€Ί random-number
Python Program to Generate a Random Number
October 18, 2014 - To generate random number in Python, randint() function is used. This function is defined in random module. # Program to generate a random number between 0 and 9 # importing the random module import random print(random.randint(0,9))
🌐
HowStuffWorks
computer.howstuffworks.com β€Ί question697.htm
How can a totally logical computer generate a random number? | HowStuffWorks
2 weeks ago - The random_seed variable is multiplied by 1,103,515,245 and then 12,345 gets added to the product; random_seed is then replaced by this new value. This is actually a pretty good pseudo-random number generator. It has a good distribution and it is non-repeating. If you use it to produce random numbers between 0 and 9, here are the first 20 values that it produces if the seed is 10:
Top answer
1 of 16
4922

There are some examples on the Mozilla Developer Network page:

/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive).
 * The value is no lower than min (or the next integer greater than min
 * if min isn't an integer) and no greater than max (or the next integer
 * lower than max if max isn't an integer).
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Here's the logic behind it. It's a simple rule of three:

Math.random() returns a Number between 0 (inclusive) and 1 (exclusive). So we have an interval like this:

[0 .................................... 1)

Now, we'd like a number between min (inclusive) and max (exclusive):

[0 .................................... 1)
[min .................................. max)

We can use the Math.random to get the correspondent in the [min, max) interval. But, first we should factor a little bit the problem by subtracting min from the second interval:

[0 .................................... 1)
[min - min ............................ max - min)

This gives:

[0 .................................... 1)
[0 .................................... max - min)

We may now apply Math.random and then calculate the correspondent. Let's choose a random number:

                Math.random()
                    |
[0 .................................... 1)
[0 .................................... max - min)
                    |
                    x (what we need)

So, in order to find x, we would do:

x = Math.random() * (max - min);

Don't forget to add min back, so that we get a number in the [min, max) interval:

x = Math.random() * (max - min) + min;

That was the first function from MDN. The second one, returns an integer between min and max, both inclusive.

Now for getting integers, you could use round, ceil or floor.

You could use Math.round(Math.random() * (max - min)) + min, this however gives a non-even distribution. Both, min and max only have approximately half the chance to roll:

min...min+0.5...min+1...min+1.5   ...    max-0.5....max
β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€ ... β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”¬β”€β”€β”˜   ← Math.round()
   min          min+1                          max

With max excluded from the interval, it has an even less chance to roll than min.

With Math.floor(Math.random() * (max - min +1)) + min you have a perfectly even distribution.

 min...  min+1...    ...      max-1... max....   (max+1 is excluded from interval)
β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€ ... β”˜β””β”€β”€β”€β”¬β”€β”€β”€β”˜β””β”€β”€β”€β”¬β”€β”€β”€β”˜   ← Math.floor()
   min     min+1               max-1    max

You can't use ceil() and -1 in that equation because max now had a slightly less chance to roll, but you can roll the (unwanted) min-1 result too.

2 of 16
657
var randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
🌐
W3Schools
w3schools.com β€Ί js β€Ί js_random.asp
JavaScript Random
In other words, the range is [0, 9]. // Return a random integer from 0 to 10 (both included): Math.floor(Math.random() * 11); Try it Yourself Β» Β· // Return a random integer from 0 to 99 (both included): Math.floor(Math.random() * 100); Try it Yourself Β» Β· // Return a random integer from 0 to 100 (both included): Math.floor(Math.random() * 101); Try it Yourself Β» Β· // Return a random integer between 1 and 10 (both included): Math.floor(Math.random() * 10) + 1; Try it Yourself Β»
🌐
RANDOM.ORG
random.org
RANDOM.ORG - True Random Number Service
Integer Generator makes random numbers in configurable intervals Sequence Generator will randomize an integer sequence of your choice Integer Set Generator makes sets of non-repeating integers Gaussian Generator makes random numbers to fit a normal distribution Decimal Fraction Generator makes numbers in the [0,1] range with configurable decimal places Raw Random Bytes are useful for many cryptographic purposes
🌐
freeCodeCamp
forum.freecodecamp.org β€Ί guide
freeCodeCamp Challenge Guide: Generate Random Whole Numbers within a Range - Guide - The freeCodeCamp Forum
October 16, 2019 - Generate Random Whole Numbers within a Range Hints Hint 1 randomRange should use both myMax and myMin, and return a random number in your range. You cannot pass the test if you are only re-using the function ourRandomRange inside your randomRange formula. You need to write your own formula ...
🌐
W3Schools
w3schools.com β€Ί python β€Ί numpy β€Ί numpy_random.asp
Introduction to Random Numbers in NumPy
In this tutorial we will be using pseudo random numbers. NumPy offers the random module to work with random numbers. ... The random module's rand() method returns a random float between 0 and 1.
🌐
University of Utah
users.cs.utah.edu β€Ί ~germain β€Ί PPS β€Ί Topics β€Ί random_numbers.html
Programming - Random Numbers
In the case of Matlab and C, this generator is the "rand()" function. In the case of Java or Actionscript there is a random function associated with the Math library. Matlabs random number generation function is called rand. In Matlab, the rand function returns a floating point number between 0 and 1 (e.g., .01, .884, .123, etc).
🌐
Calculator.net
calculator.net β€Ί home β€Ί math β€Ί random number generator
Random Number Generator
Two free random number generators that work in user-defined min and max range. Both random integers and decimal numbers can be generated with high precision.
🌐
CalculatorSoup
calculatorsoup.com β€Ί calculators β€Ί statistics β€Ί random-number-generator.php
Random Number Generator
January 27, 2026 - Random number generator for numbers 0 to 1,000,000. Generate positive or negative random numbers or random number lists with repeats or no repeats.
🌐
Studytonight
studytonight.com β€Ί python-howtos β€Ί how-to-generate-random-integers-between-0-and-9
How to Generate random integers between 0 and 9 - Studytonight
February 2, 2021 - This function belongs to random module and it generates random integers between 0 and 9. The randrange() accepts three parameters - start, stop, and step. This function returns a random integer within a range.