Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



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*


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


Fuchsia is an open-source capability-based operating system developed by Google. —Wikipedia


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


Philosophically, there is no such thing as a random number


There are ways to get truly random numbers into a computer. Certain phenomena are theorized to be fundamentally random, and can be used as inputs to hardware.

https://en.m.wikipedia.org/wiki/Hardware_random_number_gener...


The sound of conviction in your first sentence does not match the "theorized to be" in the second sentence. I recommend that you don't bring a "for all intents and purposes" to a "philosophically" fight. ;)


However, the universe is based on randomness at its quantum core.


Or perhaps the universe is a simulation whose prng was seeded with 1 just like golang does

Maybe we're in the first batch of simulations, and the tester came along and asks why they're all identical. The cosmic coder then realises that they forgot to call the function to seed the prng.


Is it though? I’m merely a layperson here so I might be grossly misunderstanding, but I didn’t know determinism had been ruled out by quantum physics. I was under the impression that quantum phenomena was best described using probability. That means there might be an element of true randomness going on, but also that these systems are so chaotic that an observation is going to have some noise, regardless of any randomness at the core. The latter says nothing about how random things are, merely that they appear with some probability, they could be completely deterministic for all we know.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: