Hi I make a app and want to know can we use random
module to generate a random number which will change every 24 hours?
Thanks for help.

- 27
- 2
-
1Yes, the random module creates random numbers – Sayse Sep 30 '21 at 17:00
-
That contradicts the actual purpose of the module a bit, don't you think? Look into `random.seed()` but the purpose is a bit strange. – Jan Sep 30 '21 at 17:01
-
Can any other module could be usefull? – Aarsh Raghuvanshi Sep 30 '21 at 17:01
-
2What's the use case of this? – pjs Sep 30 '21 at 17:02
-
Yes other modules could be useful. There's a random number module in most languages. – Rashid 'Lee' Ibrahim Sep 30 '21 at 17:02
3 Answers
You can reseed random
by using a count of days since a fixed date:
from datetime import datetime
import random
d0 = datetime(2008, 8, 18) # Pick an arbitrary date in the past
d1 = datetime.now()
delta = d1 - d0
print(delta.days)
random.seed(delta.days)
print(random.randint(1,10))
This will mean that print(random.randint(1,10))
will produce the same number anytime you run this today, but a likely different number tomorrow.
The delta
section of this code was copied from this answer on stackoverflow.

- 37,849
- 12
- 53
- 71
-
Wrap it in a function using `yield`, and only update the yielded value if elapsed time has incremented. That would give a user friendly interface. – pjs Sep 30 '21 at 17:07
-
-
I decided to post my suggestion as a separate answer after thinking about the potential impact of reseeding on distributional behavior. – pjs Oct 01 '21 at 21:32
Well, you can make use random
and datetime
module.
Here is the sample code:
import random, datetime
x = datetime.datetime.today().strftime("%Y:%m:%d")
random.seed(x)
r_int = random.randint(1, 100)
print(r_int)
Output:
73

- 280
- 1
- 11
Solutions based on reseeding the PRNG are effectively using the seed
function as your random number generator rather than the PRNG state update algorithm. Because of this, there are no guarantees regarding the distributional properties of the result, whereas a good PRNG yields a sequence of values which pass numerous statistical tests for uniformity and lack of serial correlation.
The following solution wraps things up as a generator using yield
, but the value to be yielded is only updated after the requisite period of time has passed, regardless of how many calls are made to the generator. If your app is intended to run for a multi-day extended period and you care about the distributional characteristics, I'd suggest something like this:
from datetime import datetime, timedelta
from random import randint # , seed
import time
def durable_randint(*, low = 1, high = 100, duration = timedelta(days = 1)):
assert isinstance(duration, timedelta), 'duration must be a "timedelta"!'
today = datetime.now()
# seed(today.strftime("%Y:%m:%d"))
last = today - 2 * duration
while True:
now = datetime.now()
if now - last > duration:
x = randint(low, high)
last = datetime.now()
yield x
if "__main__" == __name__:
# The following is overriding the default duration of one day.
# It will run for 45 seconds (15 iterations of 3 seconds),
# changing the value generated every 10 seconds.
gen = durable_randint(duration = timedelta(seconds = 10)) # lazy generator
for _ in range(15):
print(gen.__next__()) # next iteration of the generator
time.sleep(3)
Uncommenting the lines involving seed
will yield consistent results across multiple independent runs if the resolution is one day.

- 18,696
- 4
- 27
- 56