Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random number selection
#1
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
Reply
#2
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().
Reply
#3
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?
Reply
#4
Put the code into a function and use for loop to run it 1000 times
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Success!

Many thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,199 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  matrix number assignement to the random indices juniorcoder 4 1,916 Feb-19-2022, 02:18 PM
Last Post: juniorcoder
  Generate random hex number ZYSIA 1 11,546 Jul-16-2021, 09:21 AM
Last Post: DeaD_EyE
  Random Number Repeating Tzenesh 5 4,023 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  Random number generator charlottelol 5 3,210 Nov-10-2020, 10:51 PM
Last Post: deanhystad
  basic random number generator in replace function krug123 2 2,039 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Help with a random number generator dpcalder 2 2,326 Jun-20-2020, 03:50 AM
Last Post: buran
  Generate only one random number for many tries Bhavika 2 1,732 Mar-29-2020, 12:12 PM
Last Post: Bhavika
  Finding MINIMUM number in a random list is not working Mona 5 3,045 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  random selection of song and artist not working Unknown_Relic 2 2,346 Nov-08-2019, 02:06 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020