Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
illuna-minetest
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Illuna-Minetest
illuna-minetest
Commits
8ed467d4
Commit
8ed467d4
authored
8 years ago
by
kwolekr
Browse files
Options
Downloads
Patches
Plain Diff
PcgRandom: Fix/improve documentation
parent
dfbdb5bc
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
doc/lua_api.txt
+3
-1
3 additions, 1 deletion
doc/lua_api.txt
src/noise.cpp
+20
-11
20 additions, 11 deletions
src/noise.cpp
with
23 additions
and
12 deletions
doc/lua_api.txt
+
3
−
1
View file @
8ed467d4
...
@@ -2865,7 +2865,9 @@ It can be created via `PcgRandom(seed)` or `PcgRandom(seed, sequence)`.
...
@@ -2865,7 +2865,9 @@ It can be created via `PcgRandom(seed)` or `PcgRandom(seed, sequence)`.
* `next()`: return next integer random number [`-2147483648`...`2147483647`]
* `next()`: return next integer random number [`-2147483648`...`2147483647`]
* `next(min, max)`: return next integer random number [`min`...`max`]
* `next(min, max)`: return next integer random number [`min`...`max`]
* `rand_normal_dist(min, max, num_trials=6)`: return normally distributed random number [`min`...`max`]
* `rand_normal_dist(min, max, num_trials=6)`: return normally distributed random number [`min`...`max`]
* This is only a rough approximation of a normal distribution with mean=(max-min)/2 and variance=1
* This is only a rough approximation of a normal distribution with:
* mean = (max - min) / 2, and
* variance = (((max - min + 1) ^ 2) - 1) / (12 * num_trials)
* Increasing num_trials improves accuracy of the approximation
* Increasing num_trials improves accuracy of the approximation
### `SecureRandom`
### `SecureRandom`
...
...
This diff is collapsed.
Click to expand it.
src/noise.cpp
+
20
−
11
View file @
8ed467d4
...
@@ -93,22 +93,31 @@ u32 PcgRandom::range(u32 bound)
...
@@ -93,22 +93,31 @@ u32 PcgRandom::range(u32 bound)
// If the bound is 0, we cover the whole RNG's range
// If the bound is 0, we cover the whole RNG's range
if
(
bound
==
0
)
if
(
bound
==
0
)
return
next
();
return
next
();
/*
/*
If the bound is not a multiple of the RNG's range, it may cause bias,
This is an optimization of the expression:
e.g. a RNG has a range from 0 to 3 and we take want a number 0 to 2.
0x100000000ull % bound
Using rand() % 3, the number 0 would be twice as likely to appear.
since 64-bit modulo operations typically much slower than 32.
With a very large RNG range, the effect becomes less prevalent but
still present. This can be solved by modifying the range of the RNG
to become a multiple of bound by dropping values above the a threshold.
In our example, threshold == 4 - 3 = 1 % 3 == 1, so reject 0, thus
making the range 3 with no bias.
This loop looks dangerous, but will always terminate due to the
RNG's property of uniformity.
*/
*/
u32
threshold
=
-
bound
%
bound
;
u32
threshold
=
-
bound
%
bound
;
u32
r
;
u32
r
;
/*
If the bound is not a multiple of the RNG's range, it may cause bias,
e.g. a RNG has a range from 0 to 3 and we take want a number 0 to 2.
Using rand() % 3, the number 0 would be twice as likely to appear.
With a very large RNG range, the effect becomes less prevalent but
still present.
This can be solved by modifying the range of the RNG to become a
multiple of bound by dropping values above the a threshold.
In our example, threshold == 4 % 3 == 1, so reject values < 1
(that is, 0), thus making the range == 3 with no bias.
This loop may look dangerous, but will always terminate due to the
RNG's property of uniformity.
*/
while
((
r
=
next
())
<
threshold
)
while
((
r
=
next
())
<
threshold
)
;
;
...
...
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