0

I am very new to C++. I am basically self teaching. I came across a Hangman game project that I am using for practice. My problem is to do with the random word generation.

I know that for example int n=rand()% 10 means generate random numbers from range 0 to 10.

Now in the game there is an array with 10 elements for the ten words. What I am confused about is that if numbers from 0 to 10 is randomly generated, that would be a selection from 11 random numbers. However the array only has 10 elements (0-9).

What happens when the random generator chooses 10? Element 10 does not exist in the array, right?
So should this code not have been int n=rand()% 9 instead?

Also, could the same word be repeated before all words have been selected in the game? That would obviously not be ideal. If it could, then how do I prevent this?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Ashley Pieterse
  • 85
  • 1
  • 1
  • 10
  • If you access an array out of bounds you have *undefined behaviour*. That means all bets are off, anything can happen, etc. Obviously this isn't limited to hangman games. – juanchopanza Jan 24 '15 at 12:49
  • @juanchopanza The question isn't about array access. OP simply misunderstood modulus. While it should be closed, the dupe mark is incorrect. – Pradhan Jan 24 '15 at 12:51
  • @Pradhan You have one interpretation, I have another one. The question is a mess of irrelevant information so it is hard to know what OP is really asking. – juanchopanza Jan 24 '15 at 12:55
  • Sorry, guys. I know I asked a lot of things there in my question. It's my first I posted here. Next time I will be more to the point. But thankfully I did get my answer from AB below. So thank you everyone for helping out. Much appreciated. :D – Ashley Pieterse Jan 24 '15 at 13:21

2 Answers2

5

I know that for example int n=rand()% 10 means generate random numbers from range 0 to 10.

Not exactly. Generated range is then [0,9]. Side note: in C++11 you should use better random number generator: std::uniform_int_distribution

#include <random>
#include <iostream>

int main()
{
    std::random_device rd;
    std::mt19937 gen( rd());

    // here (0,9) means endpoints included (this is a call to constructor)
    std::uniform_int_distribution<> dis(0, 9);

    std::cout << dis(gen) << std::endl; // std::endl forces std::cout to
                                        // flush it's content, you may use '\n'
                                        // instead to buffer content  
    return 0;
}

If you try to subscript array with out-of-range index then it is a disaster named Undefined Behavior:

Undefined behavior and sequence points

What are all the common undefined behaviours that a C++ programmer should know about?

Community
  • 1
  • 1
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • Ahhh perfect! It was so simple all along. I feel like a real idiot now. And I will definitely use the generator you proposed from now on instead. I now learned something new in the process too. Thank you so much for your answer. It was exactly what I was looking for. :D – Ashley Pieterse Jan 24 '15 at 13:16
0

You misunderstand ranges and modulus in C/C++: Ranges include the first element, but (usually) not the last element. Hence, the range [0, 10) is 0,1,2,3,...,9.The modulus is mathematical, the expression x % 10 clamps the result to the range [0, 10), which is 0,1,2,3,...,9

  • You are right. I did misunderstand the ranges. Another member(AB) helped me out with it, but your answer gave me some extra insight into all of this too. Thank you! :) – Ashley Pieterse Jan 24 '15 at 13:32