srand and rand

I'm working on a Dice game for a school project and am having trouble utilizing the srand and rand functions. I know which function it is supposed to be implemented in but I'm having trouble setting it up. (NOTE: The project runs and is near completion, the only problem is getting the dice to randomize instead of staying the same. I'm not asking you to do my program for me) Here is my code, kinda long but I believe you would need to see the classes. The function I'm working on is Toss(void) in the cdie.cpp file.

(another note: pay no attention to the use of auto, I know its deprecated, I've heard it a million times but that's how our teacher taught us.)

main:
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
#include    <iostream>
#include    <cstdlib>
#include    "cdiceplayer.h"
using namespace std;


// ==== main ==================================================================
//
// ============================================================================

int     main(void)
{
    auto    int             numDice;

    // ask the user how many dice to play with
    cout << "How many dice would you like to roll? ";
    if (!(cin >> numDice))
        {
        cout << "Invalid input..." << endl;
        exit(EXIT_FAILURE);
        }

    // using the user input, create the dice player object
    auto    CDicePlayer     player = numDice;
    auto    char            response;

    // loop and let the good times roll!
    do  {
        // roll the dice and show 'em!
        player.RollDice();
        player.ShowDice();

        // display the total value
        cout << "The total is: " << player.GetTotal() << endl;

        // see if the user wishes to roll again
        cout << "Roll again? ";
        cin >> response;

        } while ('y' == tolower(response));

    cout << "Thanks for playing!" << endl;
    return  0;

}  // end of "main" 


cdie.cpp:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cstdlib>
using namespace std;
#include "cdie.h"
#include "cdiceplayer.h"

int CDicePlayer::GetTotal(void)const
{
    auto    int     index;
    auto    int     total;

    for (index = 0; index < m_diceNum; ++index)
        {
        total += m_dice -> GetVal() - 1;
        }

    return total;
}

void CDicePlayer::RollDice(void)
{
    auto    int     index;

    for (index = 0; index < m_diceNum; ++index)
        {
        m_dice -> Toss();
        }
}

void CDicePlayer::ShowDice(void)const
{
    auto    int     index;

    for (index = 0; index < m_diceNum; ++ index)
        {
        m_dice -> Draw();
        }
}

void CDie::Draw(void)const
{
    if (m_diceVal == 1)
        {
        cout << "*********" << endl;
        cout << "*       *" << endl;
        cout << "*   @   *" << endl;
        cout << "*       *" << endl;
        cout << "*********" << endl;
        }
    else if (m_diceVal == 2)
        {
        cout << "*********" << endl;
        cout << "* @     *" << endl;
        cout << "*       *" << endl;
        cout << "*     @ *" << endl;
        cout << "*********" << endl;
        }
    else if (m_diceVal == 3)
        {
        cout << "*********" << endl;
        cout << "* @     *" << endl;
        cout << "*   @   *" << endl;
        cout << "*     @ *" << endl;
        cout << "*********" << endl;
        }
    else if (m_diceVal == 4)
        {
        cout << "*********" << endl;
        cout << "* @   @ *" << endl;
        cout << "*       *" << endl;
        cout << "* @   @ *" << endl;
        cout << "*********" << endl;
        }
    else if (m_diceVal == 5)
        {
        cout << "*********" << endl;
        cout << "* @   @ *" << endl;
        cout << "*   @   *" << endl;
        cout << "* @   @ *" << endl;
        cout << "*********" << endl;
        }
    else if (m_diceVal == 6)
        {
        cout << "*********" << endl;
        cout << "* @   @ *" << endl;
        cout << "* @   @ *" << endl;
        cout << "* @   @ *" << endl;
        cout << "*********" << endl;
        }
}
void CDie::Toss(void)
{
    srand(time(NULL));

    m_diceVal = rand() % MAX_DIE_VAL + MIN_DIE_VAL;
}

CDicePlayer::~CDicePlayer(void)
{
    delete []m_dice;
}


cdie.h:
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
#ifndef CDIE_H
#define CDIE_H

// defined constants
const   int     MIN_DIE_VAL = 1;
const   int     MAX_DIE_VAL = 6;

// class declaration
class   CDie
{
public:
    // constructors
    CDie(int  initVal = MIN_DIE_VAL){m_diceVal = initVal;};

    // member functions
    void    Draw(void) const;
    int     GetVal(void) const{return m_diceVal;}
    void    Toss(void);

private:

    int m_diceVal;

};

#endif // CDIE_H 


cdiceplayer.h:
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
#ifndef CDICEPLAYER_H
#define CDICEPLAYER_H

#include    "cdie.h"

const   int     DEFAULT_NUM_DICE = 2;

class   CDicePlayer
{
public:
    // constructors
    CDicePlayer(int  numDice = DEFAULT_NUM_DICE){m_dice = new CDie[numDice];
                                                 m_diceNum = numDice;}
    ~CDicePlayer(void);

    // member functions
    int     GetTotal(void) const;
    void    RollDice(void);
    void    ShowDice(void) const;

private:

    CDie    *m_dice;
    int     m_diceNum;
    int     m_total;
};

#endif // CDICEPLAYER_H 
Last edited on
Don't call srand() so many times. Call it once at the beginning of your entire program.
I have tried that and it wont change the output. If I use 3 die, all the die will display the same number. I know I'm placing the rand() in the right function, the problem is that it randomizes one number and then uses that number for each diee. Also I can't modify my main.cpp file, should have clarified that earlier.
srand should only be called once. If you can't edit the main.cpp put it in the constructor of a class (player class).

Try placing cout objects to see exactly what your code is doing every step or add breakpoints to see what values are being held.
I put the srand in the constructor but am still having problems, even with cout statements, the output is remaining unchanged.
Topic archived. No new replies allowed.