Skip to content

Commit

Permalink
$mol_chance: normalize chances by their values sum
Browse files Browse the repository at this point in the history
  • Loading branch information
spleekz committed Jun 30, 2023
1 parent aaa7cf3 commit 3f87a35
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 81 deletions.
70 changes: 4 additions & 66 deletions chance/chance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,7 @@ namespace $ {

},

'Probability sum should not be more than 100'() {

$mol_assert_fail( ()=> {

$mol_chance(
[ 50, ()=> 1 ],
[ 50, ()=> 2 ],
[ 1, ()=> 3 ],
)

} )

},

'Probability sum should not be less than 100'() {

$mol_assert_fail( ()=> {

$mol_chance(
[ 50, ()=> 1 ],
[ 30, ()=> 2 ],
[ 10, ()=> 3 ],
)

} )

},

"Result should be one of chance's fn return values"() {
'Result should be one of chance`s fn return values'() {

const result = $mol_chance(
[ 10, ()=> 1 ],
Expand All @@ -64,51 +36,17 @@ namespace $ {

},

"100 percent chance's value should be returned every time"() {

var match_count = 0
for ( var i = 0; i < 500; ++ i ) {

const result = $mol_chance( [ 100, ()=> 0 ] )

if ( result === 0 ) match_count += 1

}

$mol_assert_equal( match_count, 500 )

},

"0 percent chances's value should never be returned"() {

var match_count = 0
for ( var i = 0; i < 500; ++ i ) {

const result = $mol_chance(
[ 60, ()=> 60 ],
[ 0, ()=> 0 ],
[ 40, ()=> 40 ],
)

if ( result === 0 ) match_count += 1

}

$mol_assert_equal( match_count, 0 )

},

"Chance value sholud be called if it is a function"() {
'Chance value sholud be called if it is a function'() {

const result = $mol_chance(
[ 100, ()=> ( { prop: false } ) ]
[ 1, ()=> ( { prop: false } ) ]
)

$mol_assert_like( result, { prop: false } )

},

"Correct type inference"() {
'Correct type inference'() {

const result = $mol_chance(
[ 10, ()=> 1 ],
Expand Down
21 changes: 7 additions & 14 deletions chance/chance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,27 @@ namespace $ {
const prob_sum = chance_list.reduce( ( sum, { 0: prob } )=> {
if (
prob < 0
|| prob > 100
|| Number.isNaN( prob )
|| prob === Infinity
) {
throw new Error( `Incorrect probability value: ${ prob }, only numbers from 0 to 100 are allowed` )
throw new Error( `Incorrect probability value: ${ prob }, bun only positive numbers are allowed` )
}

sum += prob

if ( sum > 100 ) {
throw new Error( `Incorrect probability sum: ${ sum }, but 100 is max` )
}

return sum
}, 0 )

if ( prob_sum < 100 ) {
throw new Error( `Incorrect probability sum: ${ prob_sum }, but needs 100` )
}

const random = Math.random()

var prob_total = 0
for ( var i = 0; i < chance_list.length; i ++ ) {
for( var i = 0; i < chance_list.length; ++ i ) {

var { 0: prob_raw, 1: fn } = chance_list[ i ]

const { 0: prob_percent, 1: fn } = chance_list[ i ]
var prob_percent = ( prob_raw / prob_sum ) * 100

const prob = prob_total + prob_percent / 100
var prob = prob_total + ( prob_percent / 100 )

if ( random < prob ) {

Expand All @@ -53,7 +46,7 @@ namespace $ {

}

throw new Error( `Incorrect probability sum: ${ prob_sum }, but needs 100` )
throw new Error( `Incorrect probability sum: ${ prob_sum }` )

}

Expand Down
2 changes: 1 addition & 1 deletion chance/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ const color = $mol_chance(
// in 5% will be called function to generate random color.
```

**Total sum of probabilities should be 100.**
Note: chances are normalized by their values sum. Example: chances [ 20, 50 ] will be converted to ~71% and ~29% respectively.

0 comments on commit 3f87a35

Please sign in to comment.