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
0a097b5c
Commit
0a097b5c
authored
Sep 12, 2008
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change rand names to match type names
R=r DELTA=66 (19 added, 9 deleted, 38 changed) OCL=15232 CL=15265
parent
729bc5c0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
34 deletions
+44
-34
rand.go
src/lib/rand.go
+44
-34
No files found.
src/lib/rand.go
View file @
0a097b5c
...
...
@@ -9,17 +9,11 @@
*/
package
rand
/*
export
srand // set rand state (int32)
vrand // int64 63-bits
lrand // int32 31-bits
rand // int 15-bits
vnrand // int64 % (int64)
lnrand // int32 % (int32)
nrand // int % (int)
frand; // float64 >=0.0 <1.0
*/
// rand, rand31, rand63 - return non-negative random int, int32, int64
// urand32 - return random uint32
// nrand, nrand31, nrand63 - return 0 <= random < n
// frand, frand64, frand32 - return 0 <= random float, float64, float32 < 1
const
(
...
...
@@ -84,7 +78,7 @@ srand(seed int32)
}
export
func
vrand
()
int64
rand63
()
int64
{
rng_tap
--
;
if
rng_tap
<
0
{
...
...
@@ -102,56 +96,72 @@ vrand() int64
}
export
func
lrand
()
int32
urand32
()
u
int32
{
x
:=
vrand
()
&
0x7fffffff
;
return
int32
(
x
);
return
uint32
(
rand63
()
>>
31
);
}
export
func
rand
()
int
rand
31
()
int32
{
x
:=
vrand
()
&
0x7fff
;
return
int
(
x
);
return
int32
(
rand63
()
>>
32
);
}
export
func
vnrand
(
n
int64
)
int64
rand
()
int
{
var
v
,
slop
int64
;
u
:=
uint
(
rand63
());
return
int
(
u
<<
1
>>
1
);
// clear sign bit if int == int32
}
slop
=
MASK
%
n
;
for
v
=
vrand
();
v
<=
slop
;
v
=
vrand
()
{
export
func
nrand63
(
n
int64
)
int64
{
if
n
<=
0
{
return
0
}
return
v
%
n
;
max
:=
int64
((
1
<<
63
)
-
1
-
(
1
<<
63
)
%
uint64
(
n
));
v
:=
rand63
()
for
v
>
max
{
v
=
rand63
()
}
return
v
%
n
}
export
func
lnrand
(
n
int32
)
int32
nrand31
(
n
int32
)
int32
{
v
:=
vnrand
(
int64
(
n
));
return
int32
(
v
);
return
int32
(
nrand63
(
int64
(
n
)))
}
export
func
nrand
(
n
int
)
int
{
v
:=
vnrand
(
int64
(
n
));
return
int
(
v
);
return
int
(
nrand63
(
int64
(
n
)))
}
export
func
frand
()
float64
frand
64
()
float64
{
var
x
float64
;
x
=
float64
(
vrand
())
/
float64
(
MASK
);
x
:=
float64
(
rand63
())
/
float64
(
MASK
);
for
x
>=
1
{
x
=
float64
(
vrand
())
/
float64
(
MASK
);
}
x
=
float64
(
rand63
())
/
float64
(
MASK
);
}
return
x
;
}
export
func
frand32
()
float32
{
return
float32
(
frand64
())
}
export
func
frand
()
float
{
return
float
(
frand64
())
}
func
init
()
{
...
...
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