-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
56 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
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
17 changes: 17 additions & 0 deletions
17
juicy-noise-android/app/src/main/java/com/danand/juicynoise/utils/Random.kt
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,17 @@ | ||
package com.danand.juicynoise.utils | ||
|
||
fun <T> weightedChoice(options: Map<T, Double>): T { | ||
val totalWeight = options.values.sum() | ||
val normalizedOptions = options.mapValues { it.value / totalWeight } | ||
|
||
var random = Math.random() | ||
|
||
for ((option, weight) in normalizedOptions) { | ||
if (random < weight) { | ||
return option | ||
} | ||
|
||
random -= weight | ||
} | ||
throw IllegalStateException("No option selected") | ||
} |
23 changes: 0 additions & 23 deletions
23
juicy-noise-android/app/src/main/java/com/danand/juicynoise/utils/WeightedChoice.kt
This file was deleted.
Oops, something went wrong.