
你能告诉我逻辑吗?
#include <stdio.h>#include <time.h>#include <stdlib.h>int rollDice(voID) { return (1+rand()%6) + (1+rand()%6);}int main(voID) { int roll; srand(time(NulL)); roll = rollDice(); printf("You rolled %d.\n",roll); enum Gamestatus {WON,LOST,CONTINUE}; enum Gamestatus status; while(status==CONTINUE){ printf("You are rolling again: \n"); printf("You rolled %d\n",roll = rollDice()); if (targetPoint==roll){ printf("You win!"); status=WON; } else if(7==roll){ printf("You lost!"); status=LOST; } else status=CONTINUE; } return 0;}@R_419_6120@ 假设你有数百万本随机数字行的书籍. 在获得随机数之前,您需要选择一本书.
拿到书后,要获得随机数,请从书中依次读取数字.
改变这本书得到另一个随机数序列.
srand()选择一本书并从头开始随机数
rand()从所选书籍中读取下一个数字
如果将srand()放在循环中,则实际上是从同一本书的开头重新启动随机数序列.
解决方案:选择一本书一次,并始终从中读取数字.
在C程序中,如果你不“选择一本书”,随机数来自书#1,换句话说,在没有srand()调用的情况下,函数rand()的行为就像srand一样( 1)被称为.
总结以上是内存溢出为你收集整理的在rollDice函数内调用srand(time(NULL))时出现问题全部内容,希望文章能够帮你解决在rollDice函数内调用srand(time(NULL))时出现问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)