Random Number From Array

closed account (3vX4LyTq)
I want to pick a number for my array, and then add one to the corresponding variable. So, I think I'm doing it wrong because it outputs:

You got 0 one's
You got 0 two's
You got 0 three's
You got 0 four's
You got 0 five's
You got 0 six's

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
  int main() {
    srand(time(NULL));
    int dice[6] = {1,2,3,4,5,6};
    int number = 0;
    int one = 0;
    int two = 0;
    int three = 0;
    int four = 0;
    int five = 0;
    int six = 0;
    cout << "How many times would you like me to roll the dice?";
    cin >> number;
    for(int i=0;i>number;i++){
      if(dice[rand() % 7] == 0){
        one++;
      }
      else if(dice[rand() % 7] ==1){
        two++;
      }
      else if(dice[rand() % 7] == 2){
        three++;
      }
      else if(dice[rand() % 7] == 3){
        four++;
      }
      else if(dice[rand() % 7] == 4){
        five++;
      }
      else if(dice[rand() % 7] == 5){
        six++;
      }
    }
    cout << "You got " << one << " one's\n";
    cout << "You got " << two << " two's\n";
    cout << "You got " << three <<" three's\n";
    cout << "You got " << four << " four's\n";
    cout << "You got " << five << " five's\n";
    cout << "You got " << six << " six's\n";
    
    
}


So, any better ways of doing this?
Are you sure for(int i =0; i > number; i++) is what you mean?
So long as number is greater than 0, the execution will never enter the for loop.
If number is less than 0, the execution will never leave the for loop.
The correct for-loop should be :
for(int i=0;i < number;i++){

Line 14 : There are dice from 1 - 6 not zero. You cannot compare it with zero.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
      if(dice[rand() % 7] == 1){
        one++;
      }
      else if(dice[rand() % 7] == 2){
        two++;
      }
      else if(dice[rand() % 7] == 3){
        three++;
      }
      else if(dice[rand() % 7] == 4){
        four++;
      }
      else if(dice[rand() % 7] == 5){
        five++;
      }
      else if(dice[rand() % 7] == 6){
        six++;
      }
There are only six elements also, so you use rand % 6 instead.
closed account (3vX4LyTq)
Thanks mantorr... I should have checked the inequality. :)

jared
closed account (E0p9LyTq)
So, any better ways of doing this?


Yes. (Something quick and dirty using the C++ random library)

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

std::mt19937 initialize_twister(std::size_t seed = std::chrono::system_clock::now().time_since_epoch().count())
{
   static constexpr std::size_t NUM_DISCARD = 10240;

   std::minstd_rand lcg(seed);
   lcg.discard(NUM_DISCARD);

   std::size_t seeds[std::mt19937::state_size];
   std::generate_n(seeds, std::mt19937::state_size, lcg);

   try
   {
      // check if there is a random_device available
      seeds[0] = std::random_device {}();
   }
   catch (const std::exception&)
   {
      /* ignore */
   }

   std::seed_seq seed_sequence(std::begin(seeds), std::end(seeds));

   return  std::mt19937 {seed_sequence}; // warm-up with seed seed_sequence.generate()
//   return seed_sequence;
}

int main()
{
   std::mt19937 generator = initialize_twister();

   // set a distribution range (0 - 5) // die pips 1 - 6 adjusted for array
   std::uniform_int_distribution<int> distribution(0, 5);

   // create an array to hold die rolls and initialize to zero
   std::array<unsigned, 6> die = { 0 };
   
   std::cout << "How many die rolls? ";
   unsigned die_rolls = 0;
   std::cin >> die_rolls;
   std::cout << '\n';

   // roll the die
   for (unsigned i = 0; i < die_rolls; i++)
   {
      // stores the random number
      unsigned numPicked = distribution(generator);
      
      die[numPicked]++;
   }
   
   std::cout << "After " << die_rolls << " die rolls:\n";
   for (unsigned loop = 0; loop < 6; loop++)
   {
       std::cout << "Pip " << loop + 1 << ": " << die[loop] << '\n';
   }
   
   std::cout << '\n';
}

How many die rolls? 200

After 200 die rolls:
Pip 1: 42
Pip 2: 32
Pip 3: 36
Pip 4: 34
Pip 5: 21
Pip 6: 35


The for loop could be shortened to this:
49
50
51
52
for (unsigned i = 0; i < die_rolls; i++)
{
   die[distribution(generator)]++;
}
Last edited on
>
So, any better ways of doing this?


This is probably overkill for the given code but it would generalize nicely for other situations. Instead of creating 13 ints (the array and a counter for each number), why not use the index of the array to signify the number, and the value stored to signify the count?

Ex:
1
2
3
4
5
6
int counts[6] = 0;

cin >> number;
for(unsigned int i = 0; i < number; i++) {
     counts[rand() % 6]++;
}


This will also reduce the number of times rand() is called (each time returning a different number) from 6 times per iteration to once per iteration, as well as eliminating all conditionals. You could also do this by just creating a variable to store the random number instead of calling it so many times, which would also make the loop more efficient as each iteration would guarantee a <number>++ statement would be executed.
Last edited on
closed account (3vX4LyTq)
WOW, I'm just a beginner but you guys are SUPER helpful!

I didn't even know c++ had a Random library.

jared
closed account (E0p9LyTq)
I didn't even know c++ had a Random library.

Many instructors don't know that either, or choose to ignore it.

A couple of articles why using the C-library random functions is not a good idea:
http://www.gztscf.com/0716/c-srand-does-not-give-same-sequences-of-random-numbers/

http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

Here's a C++ working paper you might want to read:
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3551.pdf

Good Luck!
Last edited on
@FurryGuy : What is so good about your code? Your code is long and hard to read.

Our instructors also taught us rand(), and it is 1000x easier to use.
closed account (E0p9LyTq)
@Sanboro,

Your instructors are IMO unwilling to learn current C++ standards or are idiots. Even the C Standard recommends to NOT use srand() and rand(), they are unreliable and buggy.

Read my second link for details why using the C-library random functions is a bad idea.
http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

One of the reasons why you consider my source hard to read is you have not been exposed to the C++ random library. That is only going to hurt you later on.

Trying to win a Formula One race with a Ford Model-T is not likely.
Oh I did not know rand() can be so bad. I will bear with this semester so that I can freely use modern C++ random stuff when my vacation comes.
Topic archived. No new replies allowed.