Implementing chance in C++

Jul 21, 2008 at 11:27pm
Hello,
I was wondering if it is possible to implement chance in C++. Can you create something that says, 1, 2, 3, and 4: they each have a 25% chance of being picked, and will be completely random each time. I looked a little at rand() and srand() but this is only going to be a few numbers at a time, and I want them to have an equal chance of being picked, Thanks,

enduser000
Jul 22, 2008 at 1:16am
rand() doesn't return your number, but...

1
2
3
int choices = 4;

int pickedChoice = (int)(rand() * choices)


rand() returns a long decimal that can be multiplied by how many outcomes you want. It will be a number between 0 and 4 in the above, and the (int) conversion chops off the rest and rounds it up or down.
Jul 22, 2008 at 5:01am
not quite correct. almost
What youw ant to do, is modulo the rand() by 4. this will give you a number between 0 and 3. then add 1 to it and have a number between 1 and 4.

int pickedChoice = (int)(rand() % 4) + 1;

Here's a program where I used it
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
#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
    long Secret = 0, User = 0;
    const long max = 1000, min = 1;
    int play = 0, guesses = 0;

    while (true)
    {
       cout << "1. Play\n2. Exit\n>";
       cin >> play;
       if (play == 2) { return(0); }
       if (play != 1) { cout << "Not a valid choice!\n\n"; continue; }

       srand(time(NULL));
       Secret = (rand() % (max - min + 1)) + min;
       guesses = 0;

       cout << "\nGuess My Number (" << min << " to " << max << ")\n\n>";

       while (Secret != User)
       {
          cin >> User;
          guesses++;

          if (Secret > User) {cout << "Higher\n\n>";}
          else if (Secret < User) {cout << "Lower\n\n>";}
       }
       cout << "Congratulations. You did it in " << guesses << " tries." << endl << endl;
    }    

    system("PAUSE");
    return EXIT_SUCCESS;
}


This works fine.
Last edited on Jul 22, 2008 at 5:03am
Jul 22, 2008 at 5:19am
It's cool, Aakanaar. I learned a lot from you.
Jul 22, 2008 at 4:59pm
That's good, but I'm lookingfor something that will basically pick a number between 1 and 4 with a 25% chance each time of picking each, how would you do that?

I was running the program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int move=1000;

	srand(time());
	move = (rand() % 4);

	cout << "My move is " << move << '\n';

	return 0;
}

a bunch of times in a row and it went: 3, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0.
but if I waited awhile it worked, this will be fine for my program.

Would you mind explaning how srand() works and what kind of argumnts it can take? It looks like it'd be useful for probability but I don't exactly know how to use it except with NULL and using it before rand().
Jul 22, 2008 at 7:53pm
srand() seeds your random number generator. The number you put in decides on the sequence of "random" numbers that are drawn. If you use the same seed then the same sequence of numbers will be drawn each time.

If you execute it multiple times at a high speed then you will likely end up with the same seed and thus, the same numbers. But in theory, each time you make a rand() call you have 25% chance that it'll be 1-4
Topic archived. No new replies allowed.