From: Charlie Brej Date: Mon, 25 Jan 2010 20:31:17 +0000 (+0000) Subject: [script] Add a random number generator X-Git-Tag: 0.8.0~77 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bef862fa9287b8fda76485e5161ae99600b3c85e;p=thirdparty%2Fplymouth.git [script] Add a random number generator Adds Math.Random() which returns a random number between 0 and 1. --- diff --git a/src/plugins/splash/script/script-lib-math.c b/src/plugins/splash/script/script-lib-math.c index a78221b8..e0782438 100644 --- a/src/plugins/splash/script/script-lib-math.c +++ b/src/plugins/splash/script/script-lib-math.c @@ -44,7 +44,6 @@ static script_return_t script_lib_math_double_from_double_function (script_state return script_return_obj (script_obj_new_number (reply_double)); } - static script_return_t script_lib_math_double_from_double_double_function (script_state_t *state, void *user_data) { @@ -55,6 +54,13 @@ static script_return_t script_lib_math_double_from_double_double_function (scrip return script_return_obj (script_obj_new_number (reply_double)); } +static script_return_t script_lib_math_random (script_state_t *state, + void *user_data) +{ + double reply_double = random() / ((double)RAND_MAX + 1); + return script_return_obj (script_obj_new_number (reply_double)); +} + static double double_to_int (double value) { return (double) (int) value; @@ -102,6 +108,11 @@ script_lib_math_data_t *script_lib_math_setup (script_state_t *state) double_to_int, "value", NULL); + script_add_native_function (math_hash, + "Random", + script_lib_math_random, + NULL, + NULL); script_obj_unref (math_hash); data->script_main_op = script_parse_string (script_lib_math_string, "script-lib-math.script");