Add random.seed
This commit is contained in:
parent
4c96839d1b
commit
b651f96094
@ -5,6 +5,7 @@
|
||||
*
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <kuroko/vm.h>
|
||||
#include <kuroko/util.h>
|
||||
|
||||
@ -16,6 +17,20 @@ KRK_FUNC(random,{
|
||||
return FLOATING_VAL(r);
|
||||
})
|
||||
|
||||
KRK_FUNC(seed,{
|
||||
FUNCTION_TAKES_AT_MOST(1);
|
||||
int seed;
|
||||
|
||||
if (argc > 0) {
|
||||
CHECK_ARG(0,int,krk_integer_type,_seed);
|
||||
seed = _seed;
|
||||
} else {
|
||||
seed = time(NULL);
|
||||
}
|
||||
|
||||
srand(seed);
|
||||
})
|
||||
|
||||
KrkValue krk_module_onload_random(void) {
|
||||
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
|
||||
krk_push(OBJECT_VAL(module));
|
||||
@ -23,6 +38,7 @@ KrkValue krk_module_onload_random(void) {
|
||||
KRK_DOC(module, "Functions for generating pseudo-random numbers.");
|
||||
|
||||
BIND_FUNC(module, random);
|
||||
BIND_FUNC(module, seed);
|
||||
|
||||
return krk_pop();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user