-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.cpp
More file actions
executable file
·48 lines (40 loc) · 943 Bytes
/
Copy pathrandom.cpp
File metadata and controls
executable file
·48 lines (40 loc) · 943 Bytes
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
42
43
44
45
46
47
48
#include "random.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "utils.h"
namespace ts
{
void Random::init()
{
qDebug( "Random Number Generator Initialized." );
srand( time( NULL ) );
}
double Random::private_random()
{
double div = 1.0 + RAND_MAX;
return (rand()/div);
}
int Random::number( int from, int to )
{
if( from > to )
return 0;
else if( from == to )
return from;
else
return (Utils::double2int(private_random()*( to - from )) + from);
}
int Random::dice( int num, int faces, int bonus )
{
int iRet = bonus;
for( int i = 0; i < num; i++ )
iRet += number( 1, faces );
return iRet;
}
int Random::dice( const QString& sDiceXdYplusZ )
{
int num = 0, faces = 0, bonus = 0;
sscanf( sDiceXdYplusZ.toUtf8().data(), "%dd%d+%d", &num, &faces, &bonus );
return dice( num, faces, bonus );
}
} // namespace ts