## 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

## How to set custom seed for pseudo-random number generator

The old way of doing it: randn(‘seed’,0) The new way: s = RandStream(‘mcg16807′,’Seed’,0) RandStream.setDefaultStream(s) Note that if you use the new way, rand and randn share the same stream so if you are calling both, you may find different numbers being generated compared to the old method (which has separate generators). The old method is … Read more

## Write a truly inclusive random method for javascript

This will return [0,1] inclusive: if(MATH.random() == 0) return 1; else return MATH.random(); Explanation: If the first call to random() returns 0, return 1. Otherwise, call random again, which will be [0,1). Therefore, it will return [0,1] all inclusive.

## Why is the System.Random class not static?

You wouldn’t be able to use different seeds if it were static – the Random instance keeps track of that state. By default Random uses the current time as seed, but re-using a particular seed (i.e. new Random(42)) allows you to exactly repeat the sequence of random numbers – they will always be the same … 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

## Consistent pseudo-random numbers across platforms

Something like a Mersenne Twister (from Boost.Random) is deterministic.

## 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 positive and negative numbers in Java [duplicate]

You random on (0, 32767+32768) then subtract by 32768

## 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

## Random order & pagination Elasticsearch

This should be considerably faster than both answers above and supports seeding: curl -XGET ‘localhost:9200/_search’ -d ‘{ “query”: { “function_score” : { “query” : { “match_all”: {} }, “random_score” : {} } } }’; See: https://github.com/elasticsearch/elasticsearch/issues/1170