implementation of rand()

Check out this collection of random number generators from George Marsaglia. He’s a leading expert in random number generation, so I’d be confident using anything he recommends. The generators in that list are tiny, some requiring only a couple unsigned longs as state. Marsaglia’s generators are definitely “high quality” by your standards of long period … Read more

Pick Random String From Array

The simplest way (but slow for large lists) would be to use a resizeable container like List and remove an element after picking it. Like: var names = new List<string> { “image1.png”, “image2.png”, “image3.png”, “image4.png”, “image5.png” }; int index = random.Next(names.Count); var name = names[index]; names.RemoveAt(index); return name; When your list is empty, all values … Read more

Vary range of uniform_int_distribution

Distribution objects are lightweight. Simply construct a new distribution when you need a random number. I use this approach in a game engine, and, after benchmarking, it’s comparable to using good old rand(). Also, I’ve asked how to vary the range of distribution on GoingNative 2013 live stream, and Stephen T. Lavavej, a member of … Read more

How to generate random password with PHP?

Just build a string of random a-z, A-Z, 0-9 (or whatever you want) up to the desired length. Here’s an example in PHP: function generatePassword($length = 8) { $chars=”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″; $count = mb_strlen($chars); for ($i = 0, $result=””; $i < $length; $i++) { $index = rand(0, $count – 1); $result .= mb_substr($chars, $index, 1); } return … Read more