aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/embryo/src/lib/embryo_rand.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/embryo/src/lib/embryo_rand.c')
-rw-r--r--libraries/embryo/src/lib/embryo_rand.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/libraries/embryo/src/lib/embryo_rand.c b/libraries/embryo/src/lib/embryo_rand.c
new file mode 100644
index 0000000..627f7ed
--- /dev/null
+++ b/libraries/embryo/src/lib/embryo_rand.c
@@ -0,0 +1,36 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include <stdlib.h>
6
7#include "Embryo.h"
8#include "embryo_private.h"
9
10/* exported random number api */
11
12static Embryo_Cell
13_embryo_rand_rand(Embryo_Program *ep __UNUSED__, Embryo_Cell *params __UNUSED__)
14{
15 return (Embryo_Cell)(rand() & 0xffff);
16}
17
18static Embryo_Cell
19_embryo_rand_randf(Embryo_Program *ep __UNUSED__, Embryo_Cell *params __UNUSED__)
20{
21 double r;
22 float f;
23
24 r = (double)(rand() & 0xffff) / 65535.0;
25 f = (float)r;
26 return EMBRYO_FLOAT_TO_CELL(f);
27}
28
29/* functions used by the rest of embryo */
30
31void
32_embryo_rand_init(Embryo_Program *ep)
33{
34 embryo_program_native_call_add(ep, "rand", _embryo_rand_rand);
35 embryo_program_native_call_add(ep, "randf", _embryo_rand_randf);
36}