The algorithm problem of generating random numbers according to the specified rules

< H2 > scene < / H2 >

you need to generate n random integers between 1 seconds each time, and store them in the database. The generated random numbers cannot be duplicated with the data already in the database

. < H2 > tried ideas < / H2 >
  1. use range in the code to generate a collection with 1 seconds, calculate the difference set from the data already in the database, and randomly get the elements from the difference result array.
  2. randomly generates an integer of 1 seconds and queries whether it already exists in the database, and if so, regenerates the random number until all the required data is successfully fetched.
< H2 > question < / H2 >

because the amount of data reaches the level of millions, and the operation is executed frequently, the method of idea 1 consumes too much performance resources, and idea 2 is even more undesirable because the execution time is unpredictable.
is there a more reasonable way to deal with this requirement?

Apr.05,2021

write my way of thinking. Using subtraction
, I think your s should be a fixed number. Since this is the case, all the data that can appear has been fixed
, for example,
, then all the random integers in 1 million million may also be fixed
. Now there are 1000000 pieces of data in the database, and then the user comes to take a number, take it all out, and then random an integer out. When you return this number to the user, set a field for this number in the database, for example, used:true
the next time the user comes to pick it up, you will take all the numbers from the database, but with filter conditions. Used is not true. Assign the extracted set randomly to the user, and then set the random number to the same field in the database. For example, used:true
and so on
so there should be no problem of repetition, and if you spend money, you should only check the database once, which should be faster than your 1m 2
of course, there is room for optimization, for example, all the data may not necessarily be stored in the database, redis should also be faster, and so on. I'm just talking about my thinking


time plus random number
https://www.zhihu.com/questio.

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be437a-316bb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be437a-316bb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?