Python Forum

Full Version: Random number selection
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

I'm jumping in at the deep end here (I've literally just started codeacademy).

Basically, I was wondering if you could tell me how to code the following:

I want to input two numbers and have python generate a random number between and including the defined numbers.

E.g input 1 would be 0.3 and input 2 would be 0.6 which would output something like 0.42 or 0.54 etc.

Then, I want the generated number to be usable as a variable in an equation at the end of the script (this I have successfully coded myself).

So in short, I want to generate 5 random numbers from 5 different number ranges which I can then feed in to a separate equation :)

After this, I will look at running the script many times (automatically) and dumping a huge list of numbers out :)

Thanks for your help,

Stuart
check random.uniform() from random module
Quote:random.uniform(a, b)

Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.

The end-point value b may or may not be included in the range depending on floating-point rounding in the equation a + (b-a) * random().
Ah, that was easy!

Very similar to how excel does it.

Next question, how can I run the same script say 1000 times and display the 1000 outputs?
Put the code into a function and use for loop to run it 1000 times
Success!

Many thanks!