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
2567c073
Commit
2567c073
authored
Nov 16, 2008
by
Ken Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
random permutation function
func perm(n int) *map[int]int R=r OCL=19340 CL=19340
parent
9c7b0640
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
0 deletions
+17
-0
rand.go
src/lib/rand.go
+17
-0
No files found.
src/lib/rand.go
View file @
2567c073
...
...
@@ -14,6 +14,7 @@ package rand
// urand32 - return random uint32
// nrand, nrand31, nrand63 - return 0 <= random < n
// frand, frand64, frand32 - return 0 <= random float, float64, float32 < 1
// perm gives a random permutation map[int]int
const
(
...
...
@@ -162,6 +163,22 @@ frand() float
return
float
(
frand64
())
}
export
func
perm
(
n
int
)
*
map
[
int
]
int
{
m
:=
new
(
map
[
int
]
int
);
for
i
:=
0
;
i
<
n
;
i
++
{
m
[
i
]
=
i
;
}
for
i
:=
0
;
i
<
n
;
i
++
{
j
:=
nrand
(
n
);
t
:=
m
[
i
];
m
[
i
]
=
m
[
j
];
m
[
j
]
=
t
;
}
return
m
;
}
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