Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
scikit.js/src/randUtils.ts at v1.20.0 · javascriptdata/scikit.js · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
javascriptdata
scikit.js
Repository navigation
Code
Issues
5
(5)
Pull requests
18
(18)
Discussions
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
v1.20.0
Breadcrumbs
scikit.js
/
src
/
randUtils.ts
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
174 lines (154 loc) · 4.38 KB
v1.20.0
Breadcrumbs
scikit.js
/
src
/
randUtils.ts
Copy path
Top
File metadata and controls
Code
Blame
174 lines (154 loc) · 4.38 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
*
@license
* Copyright 2021, JsData. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ==========================================================================
*/
export
function
_prng_restore
(
prng
:
any
,
xg
:
any
,
opts
:
any
)
{
let
state
=
opts
&&
opts
.
state
if
(
state
)
{
if
(
typeof
state
==
'object'
)
xg
.
copy
(
state
,
xg
)
prng
.
state
=
(
)
=>
xg
.
copy
(
xg
,
{
}
)
}
}
export
function
alea
(
seed
?:
any
,
opts
?:
any
)
{
let
xg
=
new
AleaGen
(
seed
)
let
prng
=
(
)
=>
xg
.
next
(
)
_prng_restore
(
prng
,
xg
,
opts
)
return
prng
}
class
AleaGen
{
c
:
number
s0
:
number
s1
:
number
s2
:
number
constructor
(
seed
:
any
)
{
if
(
seed
==
null
)
seed
=
+
new
Date
(
)
let
n
=
0xefc8249d
// Apply the seeding algorithm from Baagoe.
this
.
c
=
1
this
.
s0
=
mash
(
' '
)
this
.
s1
=
mash
(
' '
)
this
.
s2
=
mash
(
' '
)
this
.
s0
-=
mash
(
seed
)
if
(
this
.
s0
<
0
)
{
this
.
s0
+=
1
}
this
.
s1
-=
mash
(
seed
)
if
(
this
.
s1
<
0
)
{
this
.
s1
+=
1
}
this
.
s2
-=
mash
(
seed
)
if
(
this
.
s2
<
0
)
{
this
.
s2
+=
1
}
function
mash
(
data
:
any
)
{
data
=
String
(
data
)
for
(
let
i
=
0
;
i
<
data
.
length
;
i
++
)
{
n
+=
data
.
charCodeAt
(
i
)
let
h
=
0.02519603282416938
*
n
n
=
h
>>>
0
h
-=
n
h
*=
n
n
=
h
>>>
0
h
-=
n
n
+=
h
*
0x100000000
// 2^32
}
return
(
n
>>>
0
)
*
2.3283064365386963e-10
// 2^-32
}
}
next
(
)
{
let
{
c
,
s0
,
s1
,
s2
}
=
this
let
t
=
2091639
*
s0
+
c
*
2.3283064365386963e-10
// 2^-32
this
.
s0
=
s1
this
.
s1
=
s2
return
(
this
.
s2
=
t
-
(
this
.
c
=
t
|
0
)
)
}
copy
(
f
:
any
,
t
:
any
)
{
t
.
c
=
f
.
c
t
.
s0
=
f
.
s0
t
.
s1
=
f
.
s1
t
.
s2
=
f
.
s2
return
t
}
}
export
type
int
=
number
/**
* Creates a new random number generator, optionally using the given seed.
*
*
@param
seed (Optional) the seed to be used.
*
@returns
A new RNG method which returns floats from range `[0,1)`.
*/
export
const
createRng
=
(
seed
?:
number
)
=>
alea
(
seed
?.
toString
(
)
)
as
(
)
=>
number
/**
* Take a uniform [0,1) random number generator (RNG) function and turns it into an
* integer RNG function.
*
*
@param
rand An RNG in the range of [0,1).
*
@returns
An RNG that takes an integer range (from, until) as arguments and returns
* a random integer in the range [from, until).
*/
export
const
randInt
=
(
rand
:
(
)
=>
number
)
=>
(
from
:
int
,
until
:
int
)
=>
{
if
(
from
>=
until
)
{
throw
new
Error
(
'randInt(rng)(from,until): from must be less than until.'
)
}
const
result
=
Math
.
floor
(
from
+
(
until
-
from
)
*
rand
(
)
)
if
(
result
===
until
)
{
return
result
-
1
}
return
result
}
/**
* Generates a random latin hypercube sampling, i.e. unique samples whose
* features are evenly distributed over the range `[0,1)`. Latin hypercube
* sampling is useful to create random non-duplicate samples that are
* somewhat evenly distribute in the `[0,1)**nFeatures` hypercube.
*
*
@see
{
@link https://en.wikipedia.org/wiki/Latin_hypercube_sampling
}
*/
export
const
lhs
=
(
rand
:
(
)
=>
number
)
=>
(
nSamples
:
int
,
nFeatures
:
int
)
=>
{
const
_randInt
=
randInt
(
rand
)
const
result
:
Float64Array
[
]
=
[
]
// init columns to linspace(0,1, nSamples)
const
normalizeDiv
=
nSamples
-
1
||
1
for
(
let
i
=
nSamples
-
1
;
i
>=
0
;
i
--
)
{
const
row
=
new
Float64Array
(
nFeatures
)
row
.
fill
(
i
/
normalizeDiv
)
result
.
push
(
row
)
}
// shuffle columns independently
for
(
let
i
=
nSamples
-
1
;
i
>=
1
;
i
--
)
{
let
n
=
i
+
1
const
ri
=
result
[
i
]
for
(
let
j
=
nFeatures
-
1
;
j
>=
0
;
j
--
)
{
const
k
=
_randInt
(
0
,
n
)
const
rij
=
ri
[
j
]
ri
[
j
]
=
result
[
k
]
[
j
]
result
[
k
]
[
j
]
=
rij
}
}
return
result
}
interface
Indexable
<
T
>
{
[
i
:
number
]
:
T
readonly
length
:
number
}
export
const
shuffle
=
(
rand
:
(
)
=>
number
)
=>
<
T
>
(
array
:
Indexable
<
T
>
)
=>
{
const
_randInt
=
randInt
(
rand
)
for
(
let
i
=
array
.
length
;
i
>
1
;
i
--
)
{
const
j
=
_randInt
(
0
,
i
)
const
tmp
=
array
[
i
-
1
]
array
[
i
-
1
]
=
array
[
j
]
array
[
j
]
=
tmp
}
}
You can’t perform that action at this time.