1 | /* $Id: rand.cpp 21337 2009-07-07 14:58:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Random Numbers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2008 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/rand.h>
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/time.h>
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/assert.h>
|
---|
42 | #include <iprt/thread.h>
|
---|
43 | #include <iprt/once.h>
|
---|
44 | #include "internal/rand.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | /*******************************************************************************
|
---|
48 | * Global Variables *
|
---|
49 | *******************************************************************************/
|
---|
50 | /** For lazily initializating of the random generator. */
|
---|
51 | static RTONCE g_rtRandOnce = RTONCE_INITIALIZER;
|
---|
52 | /** The default random generator. */
|
---|
53 | static RTRAND g_hRand = NIL_RTRAND;
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Perform lazy initialization.
|
---|
58 | *
|
---|
59 | * @returns IPRT status code.
|
---|
60 | * @param pvUser1 Ignored.
|
---|
61 | * @param pvUser2 Ignored.
|
---|
62 | */
|
---|
63 | static DECLCALLBACK(int) rtRandInitOnce(void *pvUser1, void *pvUser2)
|
---|
64 | {
|
---|
65 | NOREF(pvUser1);
|
---|
66 | NOREF(pvUser2);
|
---|
67 |
|
---|
68 | RTRAND hRand;
|
---|
69 | int rc = RTRandAdvCreateSystemFaster(&hRand);
|
---|
70 | if (RT_FAILURE(rc))
|
---|
71 | rc = RTRandAdvCreateParkMiller(&hRand);
|
---|
72 | if (RT_SUCCESS(rc))
|
---|
73 | {
|
---|
74 | RTRandAdvSeed(hRand, ASMReadTSC() >> 8);
|
---|
75 | g_hRand = hRand;
|
---|
76 | }
|
---|
77 | else
|
---|
78 | AssertRC(rc);
|
---|
79 |
|
---|
80 | return rc;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | RTDECL(void) RTRandBytes(void *pv, size_t cb) RT_NO_THROW
|
---|
85 | {
|
---|
86 | RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
|
---|
87 | RTRandAdvBytes(g_hRand, pv, cb);
|
---|
88 | }
|
---|
89 | RT_EXPORT_SYMBOL(RTRandBytes);
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(uint32_t) RTRandU32Ex(uint32_t u32First, uint32_t u32Last) RT_NO_THROW
|
---|
93 | {
|
---|
94 | RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
|
---|
95 | return RTRandAdvU32Ex(g_hRand, u32First, u32Last);
|
---|
96 | }
|
---|
97 | RT_EXPORT_SYMBOL(RTRandU32Ex);
|
---|
98 |
|
---|
99 |
|
---|
100 | RTDECL(uint32_t) RTRandU32(void) RT_NO_THROW
|
---|
101 | {
|
---|
102 | RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
|
---|
103 | return RTRandAdvU32(g_hRand);
|
---|
104 | }
|
---|
105 | RT_EXPORT_SYMBOL(RTRandU32);
|
---|
106 |
|
---|
107 |
|
---|
108 | RTDECL(int32_t) RTRandS32Ex(int32_t i32First, int32_t i32Last) RT_NO_THROW
|
---|
109 | {
|
---|
110 | RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
|
---|
111 | return RTRandAdvS32Ex(g_hRand, i32First, i32Last);
|
---|
112 | }
|
---|
113 | RT_EXPORT_SYMBOL(RTRandS32Ex);
|
---|
114 |
|
---|
115 |
|
---|
116 | RTDECL(int32_t) RTRandS32(void) RT_NO_THROW
|
---|
117 | {
|
---|
118 | RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
|
---|
119 | return RTRandAdvS32(g_hRand);
|
---|
120 | }
|
---|
121 | RT_EXPORT_SYMBOL(RTRandS32);
|
---|
122 |
|
---|
123 |
|
---|
124 | RTDECL(uint64_t) RTRandU64Ex(uint64_t u64First, uint64_t u64Last) RT_NO_THROW
|
---|
125 | {
|
---|
126 | RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
|
---|
127 | return RTRandAdvU64Ex(g_hRand, u64First, u64Last);
|
---|
128 | }
|
---|
129 | RT_EXPORT_SYMBOL(RTRandU64Ex);
|
---|
130 |
|
---|
131 |
|
---|
132 | RTDECL(uint64_t) RTRandU64(void) RT_NO_THROW
|
---|
133 | {
|
---|
134 | RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
|
---|
135 | return RTRandAdvU64(g_hRand);
|
---|
136 | }
|
---|
137 | RT_EXPORT_SYMBOL(RTRandU64);
|
---|
138 |
|
---|
139 |
|
---|
140 | RTDECL(int64_t) RTRandS64Ex(int64_t i64First, int64_t i64Last) RT_NO_THROW
|
---|
141 | {
|
---|
142 | RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
|
---|
143 | return RTRandAdvS64Ex(g_hRand, i64First, i64Last);
|
---|
144 | }
|
---|
145 | RT_EXPORT_SYMBOL(RTRandS64Ex);
|
---|
146 |
|
---|
147 |
|
---|
148 | RTDECL(int64_t) RTRandS64(void) RT_NO_THROW
|
---|
149 | {
|
---|
150 | RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
|
---|
151 | return RTRandAdvS32(g_hRand);
|
---|
152 | }
|
---|
153 | RT_EXPORT_SYMBOL(RTRandS64);
|
---|
154 |
|
---|