Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open source random number generation algorithm in C++? [closed]

I need to generate random numbers in the range 1 - 10000 continuously with out duplication. Any recommendations?

Description: we are building a new version for our application, which maintains records in Sqlite DB. in the last version of our application, we did not had unique key for each record. But now with new upgraded version, we need to support the import facility from the last version's DB. So what we do is, we read each and every record from the old DB and generate a random number for the unique key and store it in new DB. Here we many need to import up to 10000 records continuously.

like image 537
TG. Avatar asked Oct 10 '08 04:10

TG.


Video Answer


2 Answers

Well, eventually you'll either have to stop generating them, or you're going to star duplicating them.

On a computer your options are pretty limited to Pseudo Random Number Generators (PRNGs), and given your constraint that they never repeat then a PRNG is your best option - real random data will occasionally duplicate a number.

In your case, I'd consider using a large PRNG (32 bit or larger) to shuffle your 10,000 numbers, and then send the numbers out in the shuffled order.

Once they're used up you can shuffle again - since the PRNG is so large you'll be able to go through the 10k numbers many times before duplicating a sequence.

Give us more information about what your doing and we may come up with a better answer.

-Adam

like image 168
Adam Davis Avatar answered Sep 29 '22 09:09

Adam Davis


Mersenne Twister is the current best (though i could be a few weeks behind any really new discoveries). Source in just about every language is available somewhere out there, and MT is also provided in Boost here

like image 21
DarenW Avatar answered Sep 29 '22 09:09

DarenW