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