differences between random and urandom

Using /dev/random may require waiting for the result as it uses so-called entropy pool, where random data may not be available at the moment. /dev/urandom returns as many bytes as user requested and thus it is less random than /dev/random. As can be read from the man page: random When read, the /dev/random device will … Read more

Pick a random value from a Go Slice

Use function Intn from rand package to select a random index. import ( “math/rand” “time” ) // … rand.Seed(time.Now().Unix()) // initialize global pseudo random generator message := fmt.Sprint(“Gonna work from home…”, reasons[rand.Intn(len(reasons))]) Other solution is to use Rand object. s := rand.NewSource(time.Now().Unix()) r := rand.New(s) // initialize local pseudorandom generator r.Intn(len(reasons))

How many double numbers are there between 0.0 and 1.0?

Java doubles are in IEEE-754 format, therefore they have a 52-bit fraction; between any two adjacent powers of two (inclusive of one and exclusive of the next one), there will therefore be 2 to the 52th power different doubles (i.e., 4503599627370496 of them). For example, that’s the number of distinct doubles between 0.5 included and … Read more

Non-repetitive random number in numpy

numpy.random.Generator.choice offers a replace argument to sample without replacement: from numpy.random import default_rng rng = default_rng() numbers = rng.choice(20, size=10, replace=False) If you’re on a pre-1.17 NumPy, without the Generator API, you can use random.sample() from the standard library: print(random.sample(range(20), 10)) You can also use numpy.random.shuffle() and slicing, but this will be less efficient: a … Read more

Random / noise functions for GLSL

For very simple pseudorandom-looking stuff, I use this oneliner that I found on the internet somewhere: float rand(vec2 co){ return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453); } You can also generate a noise texture using whatever PRNG you like, then upload this in the normal fashion and sample the values in your shader; I can dig … Read more

How can I get a random number in Kotlin?

My suggestion would be an extension function on IntRange to create randoms like this: (0..10).random() TL;DR Kotlin >= 1.3, one Random for all platforms As of 1.3, Kotlin comes with its own multi-platform Random generator. It is described in this KEEP. The extension described below is now part of the Kotlin standard library, simply use … Read more