Skip to content

Commit

Permalink
Merge pull request #67 from Next-Flip/fix-random
Browse files Browse the repository at this point in the history
Explain RNG differences, Fix JS Math.random()
  • Loading branch information
Willy-JL authored Apr 2, 2024
2 parents b8b25b0 + c5dcea0 commit 0f5e763
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion applications/external
2 changes: 1 addition & 1 deletion applications/system/js_app/modules/js_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void js_math_random(struct mjs* mjs) {
mjs_return(mjs, MJS_UNDEFINED);
}
const uint32_t random_val = furi_hal_random_get();
double rnd = (double)random_val / RAND_MAX;
double rnd = (double)random_val / FURI_HAL_RANDOM_MAX;
mjs_return(mjs, mjs_mk_number(mjs, rnd));
}

Expand Down
6 changes: 5 additions & 1 deletion targets/furi_hal_include/furi_hal_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
extern "C" {
#endif

#define FURI_HAL_RANDOM_MAX 0xFFFFFFFF

/** Initialize random subsystem */
void furi_hal_random_init(void);

/** Get random value
* furi_hal_random_get() gives up to FURI_HAL_RANDOM_MAX
* rand() and random() give up to RAND_MAX
*
* @return random value
* @return 32 bit random value (up to FURI_HAL_RANDOM_MAX)
*/
uint32_t furi_hal_random_get(void);

Expand Down

0 comments on commit 0f5e763

Please sign in to comment.