Java – algorithm for generating random numbers of Size X

In my mobile application, I must provide users with a random and unique x alphanumeric code so that users can reply with this alphanumeric code to perform some tasks

The number of users using this application is about 1 million and the mail flow is about 100000 per day

I can only use 26 uppercase letters, 26 lowercase letters and 10 numbers If the random number size is 5, then I can generate a unique combination of 916132832 After the combination, I want to recycle this digital code again

I'm looking for an algorithm Is there any algorithm to solve this problem?

Solution

If you accept random numbers for recycling, why wait for the combination to run out before recycling?

>At the end of the combined set, this makes the numbers less and less random > this forces you to maintain some databases to know which numbers have been used and which have not been used

I only generate random numbers and don't care if I've used them

If you really want to keep it as you asked, you can do this:

>Generate all combinations and put them into the database table > store the size of this table in a variable > generate a random number r between 1 and the size of the table > get the combination stored in row r of the table > delete row r from the table and reduce the size variable > start again when the table is empty (and the size variable is 0)

You can improve it by moving used numbers from one table to another and using the second table instead of the first table when the first table is empty

You can also do it in memory if you have enough

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>