fix random numer generating; add cell_random_fit to complement cell_random)

This commit is contained in:
2025-12-01 16:26:27 -06:00
parent 139405a30b
commit 20f5e4e81c
5 changed files with 16 additions and 14 deletions

View File

@@ -682,6 +682,7 @@ stone.p = function(object)
function guid(bits = 256)
{
log.console(os.random())
var guid = new blob(bits, os.random)
stone(guid)
return text(guid,'h')

View File

@@ -501,12 +501,7 @@ int randombytes(void *buf, size_t n) {
#endif
JSC_CCALL(os_random,
double random_double;
uint8_t *buf = (uint8_t *)&random_double;
if (randombytes(buf, sizeof(double)) != 0) {
return JS_ThrowInternalError(js, "failed to generate random bytes");
}
return JS_NewInt32(js, random_double);
return JS_NewFloat64(js, cell_random_fit());
)
static const JSCFunctionListEntry js_os_funcs[] = {

View File

@@ -523,12 +523,6 @@ int JS_ArrayLength(JSContext *js, JSValue a)
return len;
}
int cell_random() {
uint8_t buf[4];
randombytes(buf, sizeof(buf));
return *(int32_t *)buf;
}
int js2bool(JSContext *js, JSValue v) { return JS_ToBool(js,v); }
JSValue bool2js(JSContext *js, int b) { return JS_NewBool(js,b); }
@@ -540,6 +534,17 @@ double js2number(JSContext *js, JSValue v) {
return g;
}
uint64_t cell_random_fit() {
uint64_t buf;
randombytes((uint8_t *)&buf, sizeof(buf));
return buf >> 11;
}
double cell_random() {
uint64_t buf = cell_random_fit();
return (double)buf / 9007199254740992.0;
}
int uncaught_exception(JSContext *js, JSValue v)
{
cell_rt *rt = JS_GetContextOpaque(js);

View File

@@ -15,8 +15,8 @@ JSValue js_new_blob_stoned_copy(JSContext *js, void *data, size_t bytes);
void *js_get_blob_data(JSContext *js, size_t *size, JSValue v);
int js_is_blob(JSContext *js, JSValue v);
int randombytes(void *buf, size_t n);
int cell_random();
double cell_random();
uint64_t cell_random_fit();
int JS_ArrayLength(JSContext *js, JSValue a);

View File

@@ -90,3 +90,4 @@ void actor_free(cell_rt *actor);
uint64_t cell_ns();
void cell_sleep(double seconds);
int randombytes(void *buf, size_t n);