random or rand function prints the same value, even in different machines

前端 未结 3 1355
深忆病人
深忆病人 2021-01-06 21:26

I want to print a random number in the range 1 to 6. I have the following code.

printf(\"The random value is %d \\n\",random(6));

It is pri

3条回答
  •  没有蜡笔的小新
    2021-01-06 21:37

    You shoud have srand() before rand() to generate new numbers each time. This code will show how do that:

    # include 
    # include 
    # inlcude 
    
    int main()
    {
        srand(time(0));
        printf("The random value is %d\n", 1 + rand() % 6);
        return 0;
    }
    

提交回复
热议问题