Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fc-pregmod
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
macaronideath
fc-pregmod
Commits
721a6d50
Commit
721a6d50
authored
5 years ago
by
Skriv
Browse files
Options
Downloads
Patches
Plain Diff
random.js
parent
51150150
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
js/random.js
+10
-10
10 additions, 10 deletions
js/random.js
with
10 additions
and
10 deletions
src/
js/random.js
→
js/random.js
+
10
−
10
View file @
721a6d50
...
...
@@ -3,22 +3,22 @@
* mean and deviation specify the desired mean and standard deviation.
* @returns {number[]}
*/
window
.
gaussianPair
=
function
(
mean
=
0
,
deviation
=
1
)
{
function
gaussianPair
(
mean
=
0
,
deviation
=
1
)
{
const
r
=
Math
.
sqrt
(
-
2.0
*
Math
.
log
(
1
-
Math
.
random
()));
const
sigma
=
2.0
*
Math
.
PI
*
(
1
-
Math
.
random
());
return
[
r
*
Math
.
cos
(
sigma
),
r
*
Math
.
sin
(
sigma
)].
map
(
val
=>
val
*
deviation
+
mean
);
}
;
}
// Generate a random integer with a normal distribution between min and max (both inclusive).
// Default parameters result in truncating the standard normal distribution between -3 and +3.
// Not specifying min/max results in rerolling val approximately 0.3% of the time.
window
.
normalRandInt
=
function
(
mean
=
0
,
deviation
=
1
,
min
=
mean
-
3
*
deviation
,
max
=
mean
+
3
*
deviation
)
{
function
normalRandInt
(
mean
=
0
,
deviation
=
1
,
min
=
mean
-
3
*
deviation
,
max
=
mean
+
3
*
deviation
)
{
let
val
=
gaussianPair
(
mean
,
deviation
)[
0
];
while
(
val
<
min
||
val
>
max
)
{
val
=
gaussianPair
(
mean
,
deviation
)[
0
];
}
return
Math
.
round
(
val
);
}
;
}
/**
* Returns a random integer between min and max (both inclusive).
...
...
@@ -27,7 +27,7 @@ window.normalRandInt = function(mean = 0, deviation = 1, min = mean - 3 * deviat
* @param {number} max
* @returns {number}
*/
window
.
jsRandom
=
function
(
min
,
max
,
count
=
1
)
{
function
jsRandom
(
min
,
max
,
count
=
1
)
{
function
rand
()
{
return
Math
.
random
()
*
(
max
-
min
+
1
)
+
min
;
}
...
...
@@ -41,7 +41,7 @@ window.jsRandom = function(min, max, count = 1) {
total
+=
rand
();
}
return
Math
.
floor
(
total
/
count
);
}
;
}
/**
* Chooses multiple random elements of an array.
...
...
@@ -49,7 +49,7 @@ window.jsRandom = function(min, max, count = 1) {
* @param {number} count
* @returns {number[]}
*/
window
.
jsRandomMany
=
function
(
arr
,
count
)
{
function
jsRandomMany
(
arr
,
count
)
{
let
result
=
[];
let
_tmp
=
arr
.
slice
();
for
(
let
i
=
0
;
i
<
count
;
i
++
)
{
...
...
@@ -57,7 +57,7 @@ window.jsRandomMany = function(arr, count) {
result
.
push
(
_tmp
.
splice
(
index
,
1
)[
0
]);
}
return
result
;
}
;
}
/**
* Accepts both an array and a list, returns undefined if nothing is passed.
...
...
@@ -65,11 +65,11 @@ window.jsRandomMany = function(arr, count) {
* @param {...(number|string|number[]|string[])} otherChoices
* @returns {number|string|number[]|string[]|undefined}
*/
window
.
jsEither
=
function
(
choices
,
...
otherChoices
)
{
function
jsEither
(
choices
,
...
otherChoices
)
{
if
(
otherChoices
.
length
===
0
&&
Array
.
isArray
(
choices
))
{
return
choices
[
Math
.
floor
(
Math
.
random
()
*
choices
.
length
)];
}
const
allChoices
=
otherChoices
;
allChoices
.
push
(
choices
);
return
allChoices
[
Math
.
floor
(
Math
.
random
()
*
allChoices
.
length
)];
}
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment