top | item 34619030

(no title)

Brentward | 3 years ago

The example in previous versions of the Go math/rand package suggested using time.Now().UnixNano() to seed the RNG source since that's essentially a random int64.

discuss

order

panzi|3 years ago

To get cryptographically grade randomness you can use one of these C functions:

*BSD, newer macOS, and GNU libc: int getentropy(void *buffer, size_t length);

Linux 3.19+: ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);

iOS and macOS 10.7+: int SecRandomCopyBytes(SecRandomRef, size_t, uint8_t *);

Fuchsia: void zx_cprng_draw(void* buffer, size_t buffer_size);

Any *nix: Read from "/dev/random" (or "/dev/urandom" under the assumption that your system has already enough entropy).

Windows: There are different functions/libraries depending on the Windows version and some of them are a complicated multi-step mess.

Some time ago I wrote a C library that abstracts that away just for fun: https://github.com/panzi/portable_get_random*

zaphirplane|3 years ago

What a fascinating insight to the naming conventions of the different OS. Mac the long descriptive. BSD short and accurate. Linux short details are for specs. Fuchisa the functional HW reference ? What is that

morepork|3 years ago

It's a reasonable choice, and will be unique for most program runs, but it's not exactly random

ihatepython|3 years ago

Philosophically, there is no such thing as a random number