Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
91653311
Commit
91653311
authored
Dec 09, 2009
by
Roger Peppe
Committed by
Russ Cox
Dec 09, 2009
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make the operations on the global rng thread safe.
R=r, rsc CC=golang-dev
https://golang.org/cl/168041
parent
3ca1b1d2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
1 deletion
+21
-1
rand.go
src/pkg/rand/rand.go
+21
-1
No files found.
src/pkg/rand/rand.go
View file @
91653311
...
@@ -5,6 +5,8 @@
...
@@ -5,6 +5,8 @@
// Package rand implements pseudo-random number generators.
// Package rand implements pseudo-random number generators.
package
rand
package
rand
import
"sync"
// A Source represents a source of uniformly-distributed
// A Source represents a source of uniformly-distributed
// pseudo-random int64 values in the range [0, 1<<63).
// pseudo-random int64 values in the range [0, 1<<63).
type
Source
interface
{
type
Source
interface
{
...
@@ -91,7 +93,7 @@ func (r *Rand) Perm(n int) []int {
...
@@ -91,7 +93,7 @@ func (r *Rand) Perm(n int) []int {
* Top-level convenience functions
* Top-level convenience functions
*/
*/
var
globalRand
=
New
(
NewSource
(
1
)
)
var
globalRand
=
New
(
&
lockedSource
{
src
:
NewSource
(
1
)}
)
// Seed uses the provided seed value to initialize the generator to a deterministic state.
// Seed uses the provided seed value to initialize the generator to a deterministic state.
func
Seed
(
seed
int64
)
{
globalRand
.
Seed
(
seed
)
}
func
Seed
(
seed
int64
)
{
globalRand
.
Seed
(
seed
)
}
...
@@ -148,3 +150,21 @@ func NormFloat64() float64 { return globalRand.NormFloat64() }
...
@@ -148,3 +150,21 @@ func NormFloat64() float64 { return globalRand.NormFloat64() }
// sample = ExpFloat64() / desiredRateParameter
// sample = ExpFloat64() / desiredRateParameter
//
//
func
ExpFloat64
()
float64
{
return
globalRand
.
ExpFloat64
()
}
func
ExpFloat64
()
float64
{
return
globalRand
.
ExpFloat64
()
}
type
lockedSource
struct
{
lk
sync
.
Mutex
;
src
Source
;
}
func
(
r
*
lockedSource
)
Int63
()
(
n
int64
)
{
r
.
lk
.
Lock
();
n
=
r
.
src
.
Int63
();
r
.
lk
.
Unlock
();
return
;
}
func
(
r
*
lockedSource
)
Seed
(
seed
int64
)
{
r
.
lk
.
Lock
();
r
.
src
.
Seed
(
seed
);
r
.
lk
.
Unlock
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment