Skip to content

Commit 753a1e7

Browse files
Jake ChampionJakeChampion
authored andcommitted
fix: make the distribution of generated numbers between 0 and 1 uniform
the previous distribution was accidentally between 0 and 0.5 due to running std::abs on an int32_t when converting to a uint32_t which had the affect of 'losing' the upper-half of the numbers able to be stored in the uint32_t
1 parent 6c351a0 commit 753a1e7

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

c-dependencies/js-compute-runtime/js-compute-builtins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4407,7 +4407,7 @@ bool math_random(JSContext *cx, unsigned argc, Value *vp) {
44074407
int32_t storage;
44084408
int32_t *buf = &storage;
44094409
random_get((int32_t)buf, 8);
4410-
uint32_t value = std::abs(storage);
4410+
uint32_t value = storage + std::pow(2, 32);
44114411
double newvalue = static_cast<float>(value) / std::powf(2.0, 32.0);
44124412

44134413
CallArgs args = CallArgsFromVp(argc, vp);

0 commit comments

Comments
 (0)