-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from domeengine/noiseRandom
[Exploration] Replacing Random implementation with noise-based RNG
- Loading branch information
Showing
9 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
[< Back](.) | ||
|
||
random | ||
============= | ||
|
||
The `random` module provides utilities for generating pseudo-random numbers, for a variety of applications. Please note, this module should not be used for applications which require a cryptographically secure source of random numbers. | ||
|
||
DOME's pseudo-random number generator is based on the "Squirrel3" noise function, described by [Squirrel Eiserloh](http://www.eiserloh.net/bio/) in [this talk](https://www.youtube.com/watch?v=LWFzPP8ZbdU). | ||
|
||
## Random | ||
|
||
### Static Methods | ||
|
||
#### `noise(x: Number): Number` | ||
Given `x` as an integer, this will return a 32-bit number based on the Squirrel3 noise function. | ||
|
||
#### `noise(x: Number, seed: Number): Number` | ||
Given `x` and `seed` as integers, this will return a 32-bit number based on the Squirrel3 noise function. The `seed` value can be used to get different outputs for the same position `x`. | ||
|
||
### Instance methods | ||
#### `construct new()` | ||
Creates a new instance of a random number generator, seeded based on the current system time. | ||
|
||
#### `construct new(seed: Number)` | ||
Creates a new instance of a random number generator, based on the provided seed value. | ||
|
||
#### `float(): Number` | ||
Returns a floating point value in the range of `0.0...1.0`, inclusive of `0.0` but exclusive of `1.0`. | ||
|
||
#### `float(end: Number): Number` | ||
Returns a floating point value in the range of `0.0...end``, inclusive of `0.0` but exclusive of `end`. | ||
|
||
#### `float(start: Number, end: Number): Number` | ||
Returns a floating point value in the range of `start...end``, inclusive of `start` but exclusive of `end`. | ||
|
||
#### `int(end: Number): Number` | ||
Returns an integer in the range `0.0...end`, inclusive of `0.0` but exclusive of `end`.` | ||
#### `int(start: Number, end: Number): Number` | ||
Returns an integer in the range `start...end`, inclusive of `start` but exclusive of `end`.` | ||
|
||
#### `sample(list: List): Any` | ||
Given a `list`, this will pick an element from that list at random. | ||
|
||
#### `sample(list: List, count: Number): List` | ||
Randomly selects `count` elements from the list and returns them in a new list. This provides "sampling without replacement", so each element is distinct. | ||
|
||
#### `shuffle(list: List): List` | ||
Uses the Fisher-Yates algorithm to shuffle the provided `list` in place. The list is also returned for convenience. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import "random" for Random | ||
import "dome" for Process | ||
|
||
class Game { | ||
static init() { | ||
for (n in 0...100) { | ||
var start = System.clock | ||
var RNG = Random.new() | ||
var n = 0 | ||
|
||
for (i in 0...1000000) { | ||
n = n + RNG.float() | ||
} | ||
n = n / 1000000 | ||
|
||
var end = System.clock | ||
System.print("Time: %(end - start) seconds") | ||
} | ||
Process.exit() | ||
} | ||
static update() {} | ||
static draw(d) {} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ declare -a arr=( | |
"plugin" | ||
"json" | ||
"platform" | ||
"random" | ||
) | ||
|
||
declare -a opts=( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
uint32_t BIT_NOISE1 = 0xB5297A4D; | ||
uint32_t BIT_NOISE2 = 0x68E31DA4; | ||
uint32_t BIT_NOISE3 = 0x1B56C4E9; | ||
uint32_t CAP = 0xFFFFFFFF; | ||
|
||
uint32_t squirrel3Hash(uint32_t position, uint32_t seed) { | ||
uint32_t mangled = position; | ||
mangled = mangled * BIT_NOISE1; | ||
mangled = mangled + seed; | ||
mangled = mangled ^ (mangled >> 8); | ||
mangled = mangled + BIT_NOISE2; | ||
mangled = mangled ^ (mangled << 8); | ||
mangled = mangled * BIT_NOISE3; | ||
mangled = mangled ^ (mangled >> 8); | ||
return mangled & CAP; | ||
} | ||
|
||
void RANDOM_noise(WrenVM* vm) { | ||
uint32_t position = wrenGetSlotDouble(vm, 1); | ||
uint32_t seed = wrenGetSlotDouble(vm, 2); | ||
wrenSetSlotDouble(vm, 0, squirrel3Hash(position, seed)); | ||
} | ||
|
||
typedef struct { | ||
uint32_t seed; | ||
uint32_t state; | ||
} RANDOM; | ||
|
||
void RANDOM_allocate(WrenVM* vm) { | ||
RANDOM* rng = wrenSetSlotNewForeign(vm, 0, 0, sizeof(RANDOM)); | ||
uint32_t seed = wrenGetSlotDouble(vm, 1); | ||
rng->seed = seed; | ||
rng->state = 0; | ||
} | ||
|
||
void RANDOM_finalize(void* data) {} | ||
|
||
void RANDOM_float(WrenVM* vm) { | ||
RANDOM* rng = wrenGetSlotForeign(vm, 0); | ||
double result = squirrel3Hash(rng->state++, rng->seed); | ||
wrenSetSlotDouble(vm, 0, result / CAP); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import "platform" for Platform | ||
|
||
class Squirrel3 { | ||
static noise(x) { noise(x, 0) } | ||
foreign static noise(x, seed) | ||
|
||
static new() { Squirrel3.new(Platform.time) } | ||
construct new(seed) {} | ||
|
||
foreign float() | ||
float(end) { float() * end } | ||
float(start, end) { start + float(end - start) } | ||
int(end) { float(end).floor } | ||
int(start, end) { float(start, end).floor } | ||
|
||
sample(list) { list[int(list.count)] } | ||
|
||
sample(list, count) { | ||
if (count > list.count) { | ||
Fiber.abort("Cannot sample more items than the list contains") | ||
} | ||
var newList = shuffle(list.toList) | ||
var end = list.count - 1 | ||
for (i in end..count) { | ||
newList.removeAt(i) | ||
} | ||
return newList | ||
} | ||
|
||
shuffle(list) { | ||
var n = list.count | ||
var j | ||
for (i in 0...n) { | ||
j = int(i + 1) | ||
list.swap(j, i) | ||
} | ||
return list | ||
} | ||
} | ||
|
||
var Random = Squirrel3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters