Bladeren bron

update dice

MX 2 jaren geleden
bovenliggende
commit
82ae267e0e
4 gewijzigde bestanden met toevoegingen van 21 en 5 verwijderingen
  1. 1 1
      ReadMe.md
  2. 7 1
      apps_source_code/dice/CHANGELOG.md
  3. 3 2
      apps_source_code/dice/README.md
  4. 10 1
      apps_source_code/dice/dice_app.c

+ 1 - 1
ReadMe.md

@@ -13,7 +13,7 @@ Apps contains changes needed to compile them on latest firmware, fixes has been
 
 The Flipper and its community wouldn't be as rich as it is without your contributions and support. Thank you for all you have done.
 
-### Apps checked & updated at `13 Aug 04:40 GMT +3`
+### Apps checked & updated at `13 Aug 21:57 GMT +3`
 
 
 # Default pack

+ 7 - 1
apps_source_code/dice/CHANGELOG.md

@@ -1,8 +1,14 @@
 # Changelog
 
+## 1.2
+
+- Fix biased randomness in dice roll (by [Robin Gareus](https://github.com/x42))
+
 ## 1.1
+
 - Add history of dice rolls
 - UI fixes
 
 ## 1
-- Initial release
+
+- Initial release

+ 3 - 2
apps_source_code/dice/README.md

@@ -1,5 +1,6 @@
-# Flipper Zero DnD Dice 
-Version: 1.1 ([Changelog](https://github.com/Ka3u6y6a/flipper-zero-dice/blob/main/CHANGELOG.md))
+# Flipper Zero DnD Dice
+
+Version: 1.2 ([Changelog](https://github.com/Ka3u6y6a/flipper-zero-dice/blob/main/CHANGELOG.md))
 
 <div style="text-align:center"><img src=".flipcorg/banner.png"/></div>
 <br />

+ 10 - 1
apps_source_code/dice/dice_app.c

@@ -5,6 +5,15 @@
 
 const Icon* draw_dice_frame;
 
+static uint16_t unbiased_rand (uint16_t max) {
+    uint16_t remainder = RAND_MAX % max;
+    uint16_t x;
+    do {
+        x = rand();
+    } while (x >= RAND_MAX - remainder);
+    return 1 + x % max;
+}
+
 static void update(State* const state) {
     if(state->app_state == SwipeLeftState) {
         for(uint8_t i = 0; i < DICE_TYPES; i++) {
@@ -67,7 +76,7 @@ static void roll(State* const state) {
 
     for(uint8_t i = 0; i < MAX_DICE_COUNT; i++) {
         if(i < state->dice_count) {
-            state->rolled_dices[i] = (rand() % dice_types[state->dice_index].type) + 1;
+            state->rolled_dices[i] = unbiased_rand(dice_types[state->dice_index].type);
             state->roll_result += state->rolled_dices[i];
         } else {
             state->rolled_dices[i] = 0;