Skip to content

Commit 6d98c18

Browse files
committed
update demo & doc
1 parent 205fbfd commit 6d98c18

File tree

3 files changed

+69
-83
lines changed

3 files changed

+69
-83
lines changed

README.md

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,54 +35,44 @@ const selectorFactory = require("random-selector");
3535

3636
1. Blindly pick out balls in a bag without returning
3737
```javascript
38-
var bagsNormal = selectorFactory.createSimpleSelectorWithoutReplacement([
38+
var normalBag = selectorFactory.createSimpleSelectorWithoutReplacement([
3939
{color:'red'},
4040
{color:'black'},
41-
{color:'red'},
4241
]);
43-
console.log("Selected ball: ", bagsNormal.select());
44-
console.log("Selected ball: ", bagsNormal.select());
45-
console.log("Selected ball: ", bagsNormal.select());
46-
console.log("Bag now empty, no ball selected: ", bagsNormal.select());
42+
console.log("Selected ball: ", normalBag.select());
43+
console.log("Selected ball: ", normalBag.select());
44+
console.log("Bag now empty, no ball left to be selected: ", normalBag.select());
4745
```
46+
4847
2. Blindly pick out balls in a bag with returning
4948
```javascript
50-
console.log("----- Simulating selecting balls from a bag with returning: ");
51-
var bagsMagic = selectorFactory.createSimpleSelectorWithoutReplacement([
52-
{color:'red'},
53-
{color:'black'},
54-
{color:'red'},
49+
var magicBag = selectorFactory.createSimpleSelectorWithReplacement([
50+
{color:'red', id:'left'},
51+
{color:'black', id:'right'},
5552
]);
56-
console.log("Selected ball: ", bagsMagic.select());
57-
console.log("Selected ball: ", bagsMagic.select());
58-
console.log("Selected ball: ", bagsMagic.select());
59-
console.log("Still have ball selected: ", bagsMagic.select());
53+
console.log("Selected ball: ", magicBag.select());
54+
console.log("Selected ball: ", magicBag.select());
55+
console.log("Still have balls: ", magicBag.select());
6056
```
57+
6158
3. Simulating rolling dice
6259
```javascript
63-
console.log("--- Simulating rolling dice: ");
64-
var diceSelector = selectorFactory.createSimpleSelectorWithReplacement([1, 2, 3, 4, 5, 6]);
65-
var points = Array();
66-
for(let i = 0;i<10;i++)
67-
{
68-
points.push(diceSelector.select());
69-
}
70-
console.log("Total points after 10 rolls: ", points);
60+
var chigurhCoin = selectorFactory.createSimpleSelectorWithReplacement(['Head', 'Tail']);
61+
console.log("Your call: ", chigurhCoin.select());
7162

7263
```
73-
3. Simulating flipping coin
64+
65+
4. Flipping coin
7466
![Image of flipping coin](./doc/img/fipping_coin.jpg)
7567
```javascript
7668
var flipSelector = selectorFactory.createSimpleSelectorWithReplacement(['Head', 'Tail']);
77-
var faces = Array();
78-
for(let i = 0;i<10;i++)
79-
{
80-
faces.push(flipSelector.select());
81-
}
82-
console.log("Coin toss result: ", faces);
69+
console.log("Toss: ", flipSelector.select());
8370
```
84-
85-
4. Simulating wheel of fortune:
71+
72+
5. [https://en.wikipedia.org/wiki/He_loves_me..._he_loves_me_not]
73+
```javascript
74+
```
75+
5. Simulating wheel of fortune:
8676
![Image Wheel of Fortune](./doc/img/wheel_fortune.jpg)
8777
```javascript
8878
var fortuneWheel = selectorFactory.createFrequencySelectorWithReplacement(
@@ -98,26 +88,19 @@ var fortuneWheel = selectorFactory.createFrequencySelectorWithReplacement(
9888
, ['600$', 10]
9989
, ['200$', 10]
10090
, ['350$', 10]
101-
, ['1000$', 10]
10291
] ///Total frequency is 1200
10392
);
104-
105-
for(let i = 0;i<10;i++)
106-
{
107-
console.log("Bonus: ", fortuneWheel.select());
108-
}
93+
console.log("Prize: ", fortuneWheel.select());
10994
```
11095
```javascript
111-
/*A modified wheel with 0.5% chance to get 1000$
112-
, 90 % chance to get 10$
113-
, 9.5% to get stuck (select return null) O_O!
114-
*/
115-
var cheatedWheel = selectorFactory.createFrequencySelectorWithReplacement(
96+
///A cheated wheel with 0.5% chance to get 1000$, 50 % chance to get 10$, 49.5% to get stuck (select return null)
97+
var realWheel = selectorFactory.createFrequencySelectorWithReplacement(
11698
[['1000$', 50]
117-
, ['10$', 9000]
99+
, ['10$', 5000]
118100
]
119-
, 10000///base is basispoint
101+
, 10000///basispoint based
120102
);
103+
console.log("Prize: ", realWheel.select());
121104
```
122105

123106

src/demo.js

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,51 @@
11
const selectorFactory = require("./index");
22

33
console.log("----- Simulating selecting balls from a bag without returning: ");
4-
var bagsNormal = selectorFactory.createSimpleSelectorWithoutReplacement([
4+
var normalBag = selectorFactory.createSimpleSelectorWithoutReplacement([
55
{color:'red'},
66
{color:'black'},
7-
{color:'red'},
87
]);
9-
console.log("Selected ball: ", bagsNormal.select());
10-
console.log("Selected ball: ", bagsNormal.select());
11-
console.log("Selected ball: ", bagsNormal.select());
12-
console.log("Bag now empty, no ball selected: ", bagsNormal.select());
8+
console.log("Selected ball: ", normalBag.select());
9+
console.log("Selected ball: ", normalBag.select());
10+
console.log("Bag now empty, no ball left to be selected: ", normalBag.select());
1311

1412
console.log("----- Simulating selecting balls from a bag with returning: ");
15-
var bagsMagic = selectorFactory.createSimpleSelectorWithoutReplacement([
16-
{color:'red'},
17-
{color:'black'},
18-
{color:'red'},
13+
var magicBag = selectorFactory.createSimpleSelectorWithReplacement([
14+
{color:'red', id:'left'},
15+
{color:'black', id:'right'},
1916
]);
20-
console.log("Selected ball: ", bagsMagic.select());
21-
console.log("Selected ball: ", bagsMagic.select());
22-
console.log("Selected ball: ", bagsMagic.select());
23-
console.log("Still have ball selected: ", bagsMagic.select());
24-
25-
26-
17+
console.log("Selected ball: ", magicBag.select());
18+
console.log("Selected ball: ", magicBag.select());
19+
console.log("Still have balls: ", magicBag.select());
2720

2821
console.log("----- Simulating rolling dice: ");
29-
var diceSelector = selectorFactory.createSimpleSelectorWithReplacement([1, 2, 3, 4, 5, 6]);
22+
var dice = selectorFactory.createSimpleSelectorWithReplacement([1, 2, 3, 4, 5, 6]);
3023
var points = Array();
3124
for(let i = 0;i<10;i++)
3225
{
33-
points.push(diceSelector.select());
26+
points.push(dice.select());
3427
}
3528
console.log("Total points after 10 rolls: ", points);
3629

37-
38-
console.log("----- Simulating flipping coin: ");
39-
var flipSelector = selectorFactory.createSimpleSelectorWithReplacement(['Head', 'Tail']);
40-
var faces = Array();
41-
for(let i = 0;i<10;i++)
30+
console.log("----- Simulating a love checker build in daisy: ");
31+
var daisy = selectorFactory.createSimpleSelectorWithoutReplacement([]);
32+
for(let i=0;i < daisy.getRandomer().getRandomIntBetween(4, 8);i++)
4233
{
43-
faces.push(flipSelector.select());
34+
daisy.getElements().push('petal');
4435
}
45-
console.log("Coin toss result: ", faces);
36+
var meter = true;
37+
while(daisy.select()!=null)
38+
{
39+
meter = !meter;
40+
console.log(meter?'He loves me':'He loves me not');
41+
}
42+
if(!meter){
43+
console.log('try another daisy');
44+
}
45+
46+
console.log("----- Simulating flipping coin: ");
47+
var chigurhCoin = selectorFactory.createSimpleSelectorWithReplacement(['Head', 'Tail']);
48+
console.log("Your call: ", chigurhCoin.select());
4649

4750
console.log("----- Simulating lucky wheel: each bonus has the same frequency");
4851
var fortuneWheel = selectorFactory.createFrequencySelectorWithReplacement(
@@ -58,22 +61,20 @@ var fortuneWheel = selectorFactory.createFrequencySelectorWithReplacement(
5861
, ['600$', 10]
5962
, ['200$', 10]
6063
, ['350$', 10]
61-
, ['1000$', 10]
6264
] ///Total frequency is 1200
6365
);
66+
console.log("Prize: ", fortuneWheel.select());
6467

65-
for(let i = 0;i<10;i++)
66-
{
67-
console.log("Bonus: ", fortuneWheel.select());
68-
}
69-
70-
///A modified wheel with 0.5% chance to get 1000$, 90 % chance to get 10$, 9.5% to get stuck (select return null) O_O!
71-
var cheatedWheel = selectorFactory.createFrequencySelectorWithReplacement(
68+
///A cheated wheel with 0.5% chance to get 1000$, 50 % chance to get 10$, 49.5% to get stuck (select return null)
69+
console.log("A cheated wheel: pretty sure that player won't get big prize!");
70+
var realWheel = selectorFactory.createFrequencySelectorWithReplacement(
7271
[['1000$', 50]
73-
, ['10$', 9000]
72+
, ['10$', 5000]
7473
]
75-
, 10000///base is basispoint
74+
, 10000///basispoint based
7675
);
76+
console.log("Prize: ", realWheel.select());
77+
7778

7879

7980

src/randomSelector.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ class RandomSelector
5656
{
5757

5858
}
59-
60-
59+
getRandomer()
60+
{
61+
return this.randomer;
62+
}
6163
}
6264
module.exports = RandomSelector;

0 commit comments

Comments
 (0)