Trying to lose at Craps

I'm writing a game of Craps and for now I'm getting about ten times more wins than losses.
Something is obviously off.
The code is simple and in my opinion cannot be simplified any further.
1
2
3
4
srand(time(0));
dice1=rand()%6+1;
dice2=rand()%6+1;
result=dice1+dice2;

Anyone has any tips on how to randomize the outcomes better?
It should be in the area of 50/50.

Thank you
Last edited on
First of all, if you're running a lot of tests to get these odds, rand() just is not up to the task and you really need to use boost's random library (which is quite a simple library to use). With rand(), you're not going to get good results if you're doing thousands (or more) tests. I'll put an example up here, if that is the problem.

Otherwise, what does the code look like which is doing your winning/losing test?

closed account (3qX21hU5)
I would highly recommend using the new C++11 #include <random> library or one like Boost like Mats suggested. They are much more up to date then using srand() and rand().

For example here is a example that uses the random library from the STL.

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
#include <iostream>
#include <random>
#include <vector>
#include <algorithm>
#include <chrono>

int main()
{
    // Get our seed
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

    // This is our default generator you can look at the random library to see what these do
    // and what other types there are.
	std::default_random_engine generator(seed);

	// This is our distribution. Since dices only have 1 through 6 we use that.
    std::uniform_int_distribution<int> distribution(1,6);

    // Just the vector to hold the total number of rolls.
    std::vector<int> rolls(10,000);

    auto diceRoll = std::bind(distribution, generator);

    // Holds the total of both the dice we roll.
    int number;

    // Rolls two dice 10,000 times. Each time we put the result into a vector element
    for (int i = 0; i != 10000; ++i)
    {
        // This is utilizing the std::bind above to do two dice rolls nad save the sum of them rolls.
        number = diceRoll() + diceRoll();

        // push the sum into a vector.
        rolls.push_back(number);
    }

    // Now we look though that vector and see how many times we have rolls a 7 or 11.
    int sevens = std::count(rolls.begin(), rolls.end(), 7);
    int elevens = std::count(rolls.begin(), rolls.end(), 11);

    // Print our results
    std::cout << "Numbers of time 7: " << sevens << " out of 10,000" << std::endl;
    std::cout << "Numbers of time 11: " << elevens << " out of 10,000" << std::endl;
    std::cout << "Numbers of times won: " << sevens + elevens << std::endl;
}


Now I admit I have no idea how to play craps so I might have got the rules wrong (If you get a seven or eleven you win?) but it shows how random can be used I hope. Though just fair warning it will take some tweaking and some work to get truly randomized outcomes.
Last edited on
Topic archived. No new replies allowed.