Shnatsel
Shnatsel

Reputation: 4209

Get a cryptographically strong random number?

How do I get a cryptographically strong random number in Erlang, equivalent to reading from /dev/random on Unix?

So far I've only seen recommendations to use random:uniform and seed it with something like now() which is not strong enough.

(Yes, I'm aware that it's pseudo-random on BSDs, but apparently they consider it good enough, and if it's not, it's their problem).

Upvotes: 0

Views: 673

Answers (2)

Ning
Ning

Reputation: 2880

This one is even better: crypto:strong_rand_bytes/1

It uses a cryptographically secure prng seeded and periodically mixed with operating system provided entropy.

If you want to convert the bytes into an integer, try crypto:bytes_to_integer(crypto:strong_rand_bytes(N)) (N is the number of bytes you want to randomly generate).

A tip for using the crypto module: use the latest release of Erlang:)

Upvotes: 8

hdima
hdima

Reputation: 3637

I think you're looking for crypto:rand_bytes/1 or crypto:rand_uniform/2

Upvotes: 2

Related Questions