Random number generator (device): Difference between revisions
Content deleted Content added
Added Mathematica |
→{{header|C}}: The first example now makes fewer assumptions about the host environment (a uint32_t isn't required for this code to work). Additionally, this code compiles as C89 and returns EXIT_FAILURE instead of 1. |
||
Line 27:
It works on systems having /dev/urandom, like [[GNU]]/[[Linux]].
<lang c>#include <
#include <
#define RANDOM_PATH "/dev/urandom"
{▼
{▼
return 1;▼
}▼
int main(void)
▲{
unsigned char buf[4];
unsigned long v;
FILE *fin;
if ((fin = fopen(RANDOM_PATH, "r")) == NULL) {
printf("%" PRIu32 "\n", v);▼
fprintf(stderr, "%s: unable to open file\n", RANDOM_PATH);
fclose(r);▼
return
if (fread(buf, 1, sizeof buf, fin) != sizeof buf) {
fprintf(stderr, "%s: not enough bytes (expected %u)\n",
RANDOM_PATH, (unsigned) sizeof buf);
return EXIT_FAILURE;
▲ }
v = buf[0] | buf[1] << 8UL | buf[2] << 16UL | buf[3] << 24UL;
}</lang>
|