Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain RNG differences, Fix JS Math.random() #67

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading