FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/function/probability/randomInt.js at develop · rpeg/mathjs · GitHub
rpeg
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
src
/
function
/
probability
/
randomInt.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
67 lines (61 loc) · 2.63 KB
Breadcrumbs
mathjs
/
src
/
function
/
probability
/
randomInt.js
Copy path
File metadata and controls
67 lines (61 loc) · 2.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import
{
factory
}
from
'../../utils/factory'
import
{
randomMatrix
}
from
'./util/randomMatrix'
import
{
createRng
}
from
'./util/seededRNG'
import
{
isMatrix
}
from
'../../utils/is'
const
name
=
'randomInt'
const
dependencies
=
[
'typed'
,
'config'
,
'?on'
]
export
const
createRandomInt
=
/* #__PURE__ */
factory
(
name
,
dependencies
,
(
{
typed
,
config
,
on
}
)
=>
{
// seeded pseudo random number generator
let
rng
=
createRng
(
config
.
randomSeed
)
if
(
on
)
{
on
(
'config'
,
function
(
curr
,
prev
)
{
if
(
curr
.
randomSeed
!==
prev
.
randomSeed
)
{
rng
=
createRng
(
curr
.
randomSeed
)
}
}
)
}
/**
* Return a random integer number larger or equal to `min` and smaller than `max`
* using a uniform distribution.
*
* Syntax:
*
* math.randomInt() // generate a random integer between 0 and 1
* math.randomInt(max) // generate a random integer between 0 and max
* math.randomInt(min, max) // generate a random integer between min and max
* math.randomInt(size) // generate a matrix with random integer between 0 and 1
* math.randomInt(size, max) // generate a matrix with random integer between 0 and max
* math.randomInt(size, min, max) // generate a matrix with random integer between min and max
*
* Examples:
*
* math.randomInt(100) // returns a random integer between 0 and 100
* math.randomInt(30, 40) // returns a random integer between 30 and 40
* math.randomInt([2, 3]) // returns a 2x3 matrix with random integers between 0 and 1
*
* See also:
*
* random, pickRandom
*
*
@param
{
Array | Matrix
} [size] If provided, an array or matrix with given
* size and filled with random values is returned
*
@param
{
number
} [min] Minimum boundary for the random value, included
*
@param
{
number
} [max] Maximum boundary for the random value, excluded
*
@return
{
number | Array | Matrix
} A random integer value
*/
return
typed
(
name
,
{
''
:
(
)
=>
_randomInt
(
0
,
1
)
,
number
:
(
max
)
=>
_randomInt
(
0
,
max
)
,
'number, number'
:
(
min
,
max
)
=>
_randomInt
(
min
,
max
)
,
'Array | Matrix'
:
(
size
)
=>
_randomIntMatrix
(
size
,
0
,
1
)
,
'Array | Matrix, number'
:
(
size
,
max
)
=>
_randomIntMatrix
(
size
,
0
,
max
)
,
'Array | Matrix, number, number'
:
(
size
,
min
,
max
)
=>
_randomIntMatrix
(
size
,
min
,
max
)
}
)
function
_randomIntMatrix
(
size
,
min
,
max
)
{
const
res
=
randomMatrix
(
size
.
valueOf
(
)
,
(
)
=>
_randomInt
(
min
,
max
)
)
return
isMatrix
(
size
)
?
size
.
create
(
res
)
:
res
}
function
_randomInt
(
min
,
max
)
{
return
Math
.
floor
(
min
+
rng
(
)
*
(
max
-
min
)
)
}
}
)
Back
|
FazBrowse Home
|
New Git URL