Login
7 branches 0 tags
Ben (X13/Arch) Added a target to build and deploy a Web REPL f6f7eed 4 years ago 134 Commits
nujel / lib / operation / random.c
/*
 * Wolkenwelten - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
 *
 * This project uses the MIT license, a copy should be included under /LICENSE
 */
#include "random.h"
#include "../collection/list.h"
#include "../misc/random-number-generator.h"
#include "../type-system.h"
#include "../type/native-function.h"
#include "../type/val.h"

static lVal *lnfRandom(lClosure *c, lVal *v){
	(void)c;
	const int n = castToInt(lCar(v),0);
	if(n == 0){
		return lValInt(rngValR());
	}else{
		return lValInt(rngValR() % n);
	}
}

static lVal *lnfRandomSeedGet(lClosure *c, lVal *v){
	(void)c;(void)v;
	return lValInt(RNGValue);
}

static lVal *lnfRandomSeedSet(lClosure *c, lVal *v){
	(void)c;
	seedRNG(castToInt(lCar(v),0));
	return NULL;
}

void lOperationsRandom(lClosure *c){
	lAddNativeFunc(c,"random",      "[&max]", "Return a random value from 0 to MAX ot INT_MAX if MAX is #nil",lnfRandom);
	lAddNativeFunc(c,"random-seed", "[]",     "Get the RNG Seed",lnfRandomSeedGet);
	lAddNativeFunc(c,"random-seed!","[seed]", "Set the RNG Seed to SEED",lnfRandomSeedSet);
}