rand() question

This is a discussion on rand() question within the C++ Programming forums, part of the General Programming Boards category; If I did Code: cout << rand()%4; would 0 be a possible outcome? 4?...

  1. #1
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    rand() question

    If I did

    Code:
    cout << rand()%4;
    would 0 be a possible outcome? 4?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Possible outcomes are 0, 1, 2, 3. Four is not possible.

    mod % returns the reminder. Therefore, 8 % 4 has a zero remainder. While the remainder of 7 % 4 is 3 and 9 % 4 is 1. Four can never be a reminder.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21