VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/rand/rand.cpp@ 16823

Last change on this file since 16823 was 11557, checked in by vboxsync, 16 years ago

iprt: RTRandAdvCreateNonPseudo -> RTRandAdvCreateSystemFaster; RTRandAdvCreatePureNonPseudo -> RTRandAdvCreateSystemTruer

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1/* $Id: rand.cpp 11557 2008-08-21 21:47:31Z 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 <iprt/time.h>
37#include <iprt/asm.h>
38#include <iprt/err.h>
39#include <iprt/assert.h>
40#include <iprt/thread.h>
41#include <iprt/once.h>
42#include "internal/rand.h"
43
44
45/*******************************************************************************
46* Global Variables *
47*******************************************************************************/
48/** For lazily initializating of the random generator. */
49static RTONCE g_rtRandOnce = RTONCE_INITIALIZER;
50/** The default random generator. */
51static RTRAND g_hRand = NIL_RTRAND;
52
53
54/**
55 * Perform lazy initialization.
56 *
57 * @returns IPRT status code.
58 * @param pvUser1 Ignored.
59 * @param pvUser2 Ignored.
60 */
61static DECLCALLBACK(int) rtRandInitOnce(void *pvUser1, void *pvUser2)
62{
63 NOREF(pvUser1);
64 NOREF(pvUser2);
65
66 RTRAND hRand;
67 int rc = RTRandAdvCreateSystemFaster(&hRand);
68 if (RT_FAILURE(rc))
69 rc = RTRandAdvCreateParkMiller(&hRand);
70 if (RT_SUCCESS(rc))
71 {
72 RTRandAdvSeed(hRand, ASMReadTSC() >> 8);
73 g_hRand = hRand;
74 }
75 else
76 AssertRC(rc);
77
78 return rc;
79}
80
81
82RTDECL(void) RTRandBytes(void *pv, size_t cb) RT_NO_THROW
83{
84 RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
85 RTRandAdvBytes(g_hRand, pv, cb);
86}
87
88
89RTDECL(uint32_t) RTRandU32Ex(uint32_t u32First, uint32_t u32Last) RT_NO_THROW
90{
91 RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
92 return RTRandAdvU32Ex(g_hRand, u32First, u32Last);
93}
94
95
96RTDECL(uint32_t) RTRandU32(void) RT_NO_THROW
97{
98 RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
99 return RTRandAdvU32(g_hRand);
100}
101
102
103RTDECL(int32_t) RTRandS32Ex(int32_t i32First, int32_t i32Last) RT_NO_THROW
104{
105 RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
106 return RTRandAdvS32Ex(g_hRand, i32First, i32Last);
107}
108
109
110RTDECL(int32_t) RTRandS32(void) RT_NO_THROW
111{
112 RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
113 return RTRandAdvS32(g_hRand);
114}
115
116
117RTDECL(uint64_t) RTRandU64Ex(uint64_t u64First, uint64_t u64Last) RT_NO_THROW
118{
119 RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
120 return RTRandAdvU64Ex(g_hRand, u64First, u64Last);
121}
122
123
124RTDECL(uint64_t) RTRandU64(void) RT_NO_THROW
125{
126 RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
127 return RTRandAdvU64(g_hRand);
128}
129
130
131RTDECL(int64_t) RTRandS64Ex(int64_t i64First, int64_t i64Last) RT_NO_THROW
132{
133 RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
134 return RTRandAdvS64Ex(g_hRand, i64First, i64Last);
135}
136
137
138RTDECL(int64_t) RTRandS64(void) RT_NO_THROW
139{
140 RTOnce(&g_rtRandOnce, rtRandInitOnce, NULL, NULL);
141 return RTRandAdvS32(g_hRand);
142}
143
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette