summaryrefslogtreecommitdiff
path: root/src/functionrandom.cpp
blob: 5599310a3add653b4341f532b3d7aaaea18414b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "functionrandom.h"

#include "gamestate.h"

#include <stdlib.h>

#ifdef WIN32
#define random() rand()
#endif

FunctionRandom::FunctionRandom()
{
}

FunctionRandom::~FunctionRandom()
{
}

void FunctionRandom::call( class GameState &gState )
{
	Variable vHigh = gState.popDeref();
	Variable vLow = gState.popDeref();

	if( vHigh.getType() != vLow.getType() )
		throw Bu::ExceptionBase("Different types in random!");

	double dRand = random()/(double)(RAND_MAX-1);
	if( vLow.getType() == Variable::tInt )
	{
		gState.push( Variable( (int64_t)(
			(dRand*(vHigh.getInt()-vLow.getInt()+1ll))+vLow.getInt()
			) ) );
	}
	else if( vLow.getType() == Variable::tFloat )
	{
		gState.push( Variable( (double)(
			(dRand*(vHigh.getFloat()-vLow.getFloat()))+vLow.getFloat()
			) ) );
	}
}