Python Forum
Random number selection - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Random number selection (/thread-4640.html)



Random number selection - stumunro - Aug-31-2017

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


RE: Random number selection - buran - Aug-31-2017

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().



RE: Random number selection - stumunro - Aug-31-2017

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?


RE: Random number selection - wavic - Aug-31-2017

Put the code into a function and use for loop to run it 1000 times


RE: Random number selection - stumunro - Aug-31-2017

Success!

Many thanks!