+ 2

How to create a program that generates binary in a list within a random time.? Let me explain

I want to code a program which generates 0 or 1 number in a list (my data) and the specificity is that first it will randomly choose 0 or 1 and second it will choose a random time between 0 and 1 second. Not clear? Well I want to make a model of the laser binary réception if the captor (my solar panel doesn't see light more than 0.2 seconds then it means it has to generate 0 otherwise 1. And so on... Hope it makes sense. Let me know if you have any suggestions or working codes! Thank you!

2nd Apr 2019, 12:54 PM
Maximilien
Maximilien  - avatar
4 odpowiedzi
+ 3
These two functions might prove useful: random.randint(0,1) # Returns 0 or 1 random.random() # Returns number in range: 0 <= x < 1 I wish you success!
2nd Apr 2019, 7:29 PM
Luis Sepúlveda
Luis Sepúlveda - avatar
+ 3
Well, I haven't made many time based codes, but you can use the time module. When the process starts (maybe when the function runs) you can have it take the current time and save it in a variable. Then generate the random time you want it to wait: start_time = time.time() time_limit = random.random() # Then make a while loop goes like this: while time.time() - start_time <= time_limit: # Generate random number This process starts when you receive Low-Light Inputs from your panel.
2nd Apr 2019, 7:41 PM
Luis Sepúlveda
Luis Sepúlveda - avatar
+ 1
Thank you! How can the time get involved?
2nd Apr 2019, 7:30 PM
Maximilien
Maximilien  - avatar
+ 1
Excellent! Thank you very much 🤗
2nd Apr 2019, 7:42 PM
Maximilien
Maximilien  - avatar