-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.jack
More file actions
49 lines (44 loc) · 1.07 KB
/
Copy pathRandom.jack
File metadata and controls
49 lines (44 loc) · 1.07 KB
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
class Random {
// random seed
static int seed;
// generate seed when key pressed
function void seed() {
var int key;
let key = 0;
// increment seed until key pressed
while(~key) {
let seed = seed + 1;
// loop around
if (seed = 32767) {
let seed = 0;
}
let key = Keyboard.keyPressed();
}
return;
}
// get a random number 0 to 32767
function int rand() {
let seed = seed + 20251;
if (seed < 0) {
let seed = seed - 32767 - 1;
}
return seed;
}
// get a number 0 to max
function int randRange(int max) {
var int mask, randValue;
let mask = 1;
while (mask < max) {
let mask = mask * 2 + 1;
}
let randValue = Random.rand() & mask;
while (randValue > max) {
let randValue = Random.rand() & mask;
}
return randValue;
}
method void dispose() {
do Memory.deAlloc(this);
return;
}
}