简体   繁体   中英

C modulo operator behaving unusually with randomly generated integer in my code

In the following code of mine, the modulo operator is used on two randomly generated numbers but the output is often incorrect. Why would that happen?

Here's an example of unexpected output:

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

void delay(int number_of_seconds)
{
    // Converting time into milli_seconds
    int milli_seconds = 1000 * number_of_seconds;

    // Storing start time
    clock_t start_time = clock();

    // looping till required time is not achieved
    while (clock() < start_time + milli_seconds)
        ;
}

void ran_dom(){             //this function generates a random number and prints its remainder
    srand(time(0));
    int x = (int) rand();
    int y = (int) rand();
    printf("x: %d\n", x);
    printf("y: %d\n", y);
    int mod_x = (x % 40);   //modulo operator with value: 40
    int mod_y = (y % 20);   //modulo operator with value: 20
    printf("x mod 40: %d\n", mod_x);
    printf("y mod 20: %d\n", mod_y);
}

void ResetScreenPosition(){     //resets screen position (in windows OS)
    COORD Position;
    HANDLE hOut;
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    Position.X = 0;
    Position.Y = 0;
    SetConsoleCursorPosition(hOut, Position);

}

void main(){
    while(1){
        ResetScreenPosition();
        ran_dom();
        delay(2);
    }
}

Thanks for taking a look!

6327 % 40 is 7 . If the screen has 23 in the position of the 7 from a previous print, printing "x mod 40: 7" will appear to have printed "x mod 40: 73" .

Try something along the lines of one of these alternatives:

printf("x mod 40: %02d \n", mod_x);
printf("[x mod 40: %d]\n", mod_x);

To my knowledge there is no standard way of clearing the screen in c. The error in your output came from overwriting the screen without clearing it.

Also there is a sleep(int seconds) function in unistd.h. Using it is probably a better idea than looping.

Calling srand() once is enough. You don't need to set your random seed each call.

This would probably be my implementation:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

void clear_screen() {
#ifdef WINDOWS
    system("cls");
#else
    /* Assume POSIX */
    system ("clear");
#endif
}

void print_random() {
    int x, y, mod_x, mod_y;

    x = rand();
    y = rand();
    printf("x: %d\n", x);
    printf("y: %d\n", y);

    mod_x = (x % 40);
    mod_y = (y % 20);
    printf("x mod 40: %d\n", mod_x);
    printf("y mod 20: %d\n", mod_y);
}

int main() {
    srand(time(NULL));
    while(1) {
        clear_screen();
        print_random();
        sleep(2);
    }
    return 0;
}

Since I don't have a Windows machine on hand right now I could only test this code on my Ubuntu. Should work though.

我刚刚发现在从 main 函数调用 delay() 之后放置这段代码也有效:

system("cls"); // refreshes screen? works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM