There are lots of different elements and two possible states for each. It is necessary that this state is randomly generated and not changed within an hour. The state of each element should not be stored in the system, but should be generated as needed. How to do it? While there is an idea like this - based on the hour number and day number (so that each day is new and does not repeat at the same time of each day) and the element name generate a random number from 1 to 10, if less than 5, then the first state, if more, the second . But how to generate a number based on another number? Or how else can one generate a constant state (one of two), so that it is the same for the whole hour? It is necessary that this works either on the server (Python, Django), or on the client (JavaScript, Angular).

I will give an example. There are elements A, B, C. And two states - 1 and 2 . Suppose, at 20:00, upon request, their status will return A-1, B-1, C-2 . If you make requests at 20:05, 20:22, 20:45 and so on, that will return anyway A-1, B-1, C-2 . But if you make a request at the next hour, for example, at 21:03, then other states will return for elements, for example, A-2, B-1, C-1 . They should not be stored somewhere, but should be generated. How to do this?

  • The number is generated on the server once per hour by cron, and stored on the client as a cookie. - Vanyamba Electronics
  • @VanyambaElectronics number should not be stored anywhere, but generated on the fly. - Vladimir37
  • Состояние каждого элемента не должно храниться в системе at this moment you started having made-up problems from nothing. 1. Store a random number on the server. 2 if it is obsolete, then update it with regular means of generating the HRP - ReinRaus
  • If the data could be generated from nothing, then according to the theory of Shannon there would be a failure for the professor, and not for the student. - Vanyamba Electronics
  • I explained. Based on the item name and the number of the current day in the month. Simply put, you pass a function string, and it creates a number from it. How to implement it? - Vladimir37

2 answers 2

You can use the seed from the range module. Change the seed once an hour and you will have new values. seed can be set based on anything, date, time, number or string.

For example:

  import random random.seed(12345) for _ in range(5): print("%.3f" % random.random()) print() random.seed(12345) for _ in range(5): print("%.3f" % random.random()) #меняем seed и получаем новую последовательность print() random.seed(54321) for _ in range(5): print("%.3f" % random.random()) 

    Decided to use the built-in hash function. I give her the name of the element and the current date, receiving in response a numeric hash that differs with different dates and the same with the same parameters.

    • one
      I'm not sure that the hash built-in function "scatters" values ​​well enough. For example ''.join([str(hash(str(i) + 'a') % 2) for i in xrange(24)]) gives us the string '101010101010101010100101' . - dzhioev