diff --git a/scripts/engine.cm b/scripts/engine.cm index 3a096911..29d4f476 100644 --- a/scripts/engine.cm +++ b/scripts/engine.cm @@ -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') diff --git a/scripts/os.c b/scripts/os.c index d2440875..3be14e71 100644 --- a/scripts/os.c +++ b/scripts/os.c @@ -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[] = { diff --git a/source/cell.c b/source/cell.c index 6cdea14b..6a35a6d8 100644 --- a/source/cell.c +++ b/source/cell.c @@ -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); diff --git a/source/cell.h b/source/cell.h index 9387c18b..237d767a 100644 --- a/source/cell.h +++ b/source/cell.h @@ -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); diff --git a/source/cell_internal.h b/source/cell_internal.h index 6c201b19..49ec397a 100644 --- a/source/cell_internal.h +++ b/source/cell_internal.h @@ -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);