-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfast_random_blocks.ts
90 lines (81 loc) · 2.45 KB
/
fast_random_blocks.ts
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
//% blockNamespace=Random color="#FF8000"
class FastRandomBlocks {
_rng: Math.FastRandom
constructor(seed: number) {
this._rng = new Math.FastRandom(seed)
}
/**
* Return the next number between 1 and 65535
*/
//% block="$this next number"
//% this.defl=rng
//% blockId="fastrandomblocks_nextnumber"
nextNumber() {
return this._rng.next()
}
/**
* Returns a random number between two numbers
* @param minimum The minimum number, ex: 1
* @param maximum The maximum number, ex: 10
*/
//% block="$this random number between $minimum and $maximum"
//% inlineInputMode=inline
//% this.defl=rng
//% blockId="fastrandomblocks_randomrange"
randomRange(minimum: number, maximum: number) {
return this._rng.randomRange(minimum, maximum)
}
/**
* Returns a boolean based on the percent passed in
* @param percent The percentage of the time the boolean returned will be true.
*/
//% block="$this $percent percent chance "
//% this.defl=rng
//% blockId="fastrandomblocks_percentchance"
percentChance(percent: number) {
return this._rng.percentChance(percent)
}
/**
* Returns a boolean with a 50% chance of being true
*/
//% block="$this 50/50 chance"
//% this.defl=rng
//% blockId="fastrandomblocks_randomboolean"
randomBoolean() {
return this._rng.randomBool()
}
/**
* Pick a random element from the array passed in
* @param elements The array to choose from, ex: ["apples", "oranges", "pears"]
*/
//% block="$this pick random from $elements"
//% elements.shadow="lists_create_with"
//% this.defl=rng
//% blockId="fastrandomblocks_randomelement"
randomElement(elements: any[]) {
return this._rng.pickRandom(elements)
}
/**
* Reset the RNG
*/
//% block="reset $this"
//% this.defl=rng
//% blockId="fastrandomblocks_resetrng"
resetRNG() {
this._rng.reset()
}
}
//% color="#FF8000"
namespace Random {
/**
* Create a RNG object
* @param seed The seed for the RNG, ex: 1234. Using 0 as the seed may have unexpected results!!!
*/
//% block="create RNG || with seed $seed"
//% blockSetVariable=rng
//% expandableArgumentMode="toggle"
//% blockId="random_createrng"
export function createRNG(seed: number = null): FastRandomBlocks {
return new FastRandomBlocks(seed);
}
}