rand question

how can i make this function give me random number each time i run it? rather than just give me the same numbers everytime
thank you

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 "Dice.h"
#include <stdlib.h>


Dice::Dice()
{
    sides = 6;
}

int Dice::roll()
{
    return (rand() % sides + 1);
}


DiceLoad::DiceLoad() {
    sides = 6;
}

DiceLoad::DiceLoad(int numSides) {
    if (numSides > 0) {
        sides = numSides;
    } else {
        sides = 6;
    }
}

Dice::Dice(int numSides)
{
    if (numSides > 0) {
        sides = numSides;
    } else {
        sides = 6;
    }
}


int DiceLoad::roll() {
    int value = (rand() % sides + 1);
    int random = (rand() % 100 + 1);

    if ((value < 6) && (random >= 95)) {
        value = value + 1;
    }

    return value;
}
You need to call srand() in main or something. The key is to only call once per program run.
http://www.cplusplus.com/reference/cstdlib/srand/
Topic archived. No new replies allowed.