sinc with TL rev. 38618.
[luatex.git] / source / libs / poppler / poppler-0.37.0 / goo / grandom.cc
blob1237175420b7a1eefc9365c9e70137f461162406
1 /*
2 * grandom.cc
4 * This file is licensed under the GPLv2 or later
6 * Pseudo-random number generation
8 * Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
9 */
11 #include <config.h>
12 #include "grandom.h"
13 #include "gtypes.h"
15 #ifdef HAVE_RAND_R // rand_r backend (POSIX)
17 static GBool initialized = gFalse;
19 #include <stdlib.h>
20 #include <time.h>
21 static unsigned int seed;
23 static void initialize() {
24 if (!initialized) {
25 seed = time(NULL);
26 initialized = gTrue;
30 void grandom_fill(Guchar *buff, int size)
32 initialize();
33 while (size--)
34 *buff++ = rand_r(&seed) % 256;
37 double grandom_double()
39 initialize();
40 return rand_r(&seed) / (1 + (double)RAND_MAX);
43 #else // srand+rand backend (unsafe, because it may interfere with the application)
45 static GBool initialized = gFalse;
47 #include <stdlib.h>
48 #include <time.h>
50 static void initialize() {
51 if (!initialized) {
52 srand(time(NULL));
53 initialized = gTrue;
57 void grandom_fill(Guchar *buff, int size)
59 initialize();
60 while (size--)
61 *buff++ = rand() % 256;
64 double grandom_double()
66 initialize();
67 return rand() / (1 + (double)RAND_MAX);
70 #endif