]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
[script] Add a random number generator
authorCharlie Brej <cbrej@cs.man.ac.uk>
Mon, 25 Jan 2010 20:31:17 +0000 (20:31 +0000)
committerroot <cbrej@cs.man.ac.uk>
Mon, 25 Jan 2010 20:31:17 +0000 (20:31 +0000)
Adds Math.Random() which returns a random number between 0 and 1.

src/plugins/splash/script/script-lib-math.c

index a78221b82afe3600c1e7843c026562feab5e277a..e0782438407f0bd9dc5782943fec64a9a3c4916d 100644 (file)
@@ -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");