鱼C论坛

 找回密码
 立即注册
查看: 4956|回复: 16

随机种子问题

[复制链接]
发表于 2012-9-23 22:05:18 | 显示全部楼层 |阅读模式
20鱼币

复制代码
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>

  4. int main(void)
  5. {
  6. int roll_count;

  7. int roll_n_dice(int dice, int sides);

  8. int dice,roll;
  9. int sides;

  10. srand((unsigned int) time(0));   这里是不是与后面的rand想对应
  11. printf("Enter the number of sides per die, 0 to stop.\n");
  12. while (scanf("%d", &sides) == 1 && sides > 0)
  13. {
  14. printf("How many dice?\n");
  15. scanf("%d", &dice);
  16. roll = roll_n_dice(dice, sides);
  17. printf("You have rolled a %d using %d %d-sided dice.\n",
  18. roll, dice, sides);
  19. printf("How many sides? Enter 0 to stop.\n");
  20. }
  21. printf("The rollem() function was called %d times.\n",
  22. roll_count);

  23. printf("GOOD FORTUNE TO YOU!\n");

  24. return 0;
  25. }
  26. int roll_count = 0;

  27. static int rollem(int sides)
  28. {
  29. int roll;

  30. roll = rand() % sides + 1;
  31. ++roll_count;

  32. return roll;
  33. }

  34. int roll_n_dice(int dice, int sides)
  35. {
  36. int d;
  37. int total = 0;
  38. if (sides < 2)
  39. {
  40. printf("Need at least 2 sides.\n");
  41. return -2;
  42. }
  43. if (dice < 1)
  44. {
  45. printf("Need at least 1 die.\n");
  46. return -1;
  47. }

  48. for (d = 0; d < dice; d++)
  49. total += rollem(sides);

  50. return total;
  51. }
  52. 我输入6 2 编译对的 然后接着输入 6 a  调试的时候sides 值是6,但是为什么会结束了呢判断条件不是while (scanf("%d", &sides) == 1 && sides > 0)
  53. 请解释
复制代码

最佳答案

查看完整内容

一个骰子六面,共三个,模拟出三个投一次投出的总点数。用的随机函数。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-9-23 22:05:19 | 显示全部楼层

一个骰子六面,共三个,模拟出三个投一次投出的总点数。用的随机函数。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-9-23 23:10:58 | 显示全部楼层
  1. srand((unsigned int) time(0));   //这是一个种子,上百度查查会给你很准确的答案
复制代码
  1. while (scanf("%d", &sides) == 1 && sides > 0)
  2. //你的while循环的条件是sides==1,当你输入6的时候循环条件表达式的值为0,即条件不满足,所以循环终止!
复制代码

点评

很好  发表于 2012-9-24 09:03
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-9-24 02:56:44 | 显示全部楼层
本帖最后由 贝壳 于 2012-9-24 03:20 编辑

可能是因为你输入的'a'是个字符,虽然程序运行时把它转化成了对应的ascii码97,但是字符和字符串默认在结尾处都带有一个'\0'的结束符,而scanf()函数在读到这个字符的时候,会返回EOF。
函数:scanf("%d", &dice);
返回的EOF刚好被while (scanf("%d", &sides) == 1 && sides > 0)接受到,这样就终止了while循环。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-9-24 03:01:27 | 显示全部楼层
你可以看一下我的运行结果:
Enter the number of sides per die, 0 to stop.
6
How many dice?
97
You have rolled a 343 using 97 6-sided dice.
How many sides? Enter 0 to stop.
6
How many dice?
a
You have rolled a 334 using 97 6-sided dice.
How many sides? Enter 0 to stop.
The rollem() function was called 19 times.
GOOD FORTUNE TO YOU!
第二次:
Enter the number of sides per die, 0 to stop.
6
How many dice?
abc
You have rolled a 21828885 using 6237016 6-sided dice.
How many sides? Enter 0 to stop.
The rollem() function was called 19 times.
GOOD FORTUNE TO YOU!

第一次你会发现输入a和输入97的随机结果非常接近;因为字符和字符串后面有结束符'\0',所以程序退出。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-9-24 10:10:54 | 显示全部楼层
贝壳 发表于 2012-9-24 03:01
你可以看一下我的运行结果:
Enter the number of sides per die, 0 to stop.
6

晕   还是不太明白 能再说说清楚吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-9-24 11:44:23 | 显示全部楼层

我去。。。不是吧。。输入6表达式怎么会是0  是1才对。。。应该是后面的A出了问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-9-24 11:45:48 | 显示全部楼层
贝壳 发表于 2012-9-24 02:56
可能是因为你输入的'a'是个字符,虽然程序运行时把它转化成了对应的ascii码97,但是字符和字符串默认在结尾 ...

好像是这个原因
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-9-24 14:44:58 | 显示全部楼层

scanf("%d", &sides)的返回值是输入数据的个数,所以这里是1.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-9-24 14:47:17 | 显示全部楼层
如果在循环里加入过滤字符和字符串的条件,程序应该就正常了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-9-24 20:14:25 | 显示全部楼层
贝壳 发表于 2012-9-24 14:47
如果在循环里加入过滤字符和字符串的条件,程序应该就正常了。

是不是 scanf("%d", &dice);
输入a后 然后后面的、\0被while (scanf("%d", &sides) == 1 && sides > 0)

接收到 所以是0.。然后循环结束
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-9-24 23:47:31 | 显示全部楼层
猪八哥 发表于 2012-9-24 20:14
是不是 scanf("%d", &dice);
输入a后 然后后面的、\0被while (scanf("%d", &sides) == 1 && sides > 0)
...

好像是。这个如果看汇编代码应该可以看出来。不过我不太会。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-9-25 00:02:10 | 显示全部楼层
虽然看不懂 不过顶了·····
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-9-25 21:08:27 | 显示全部楼层
scanf 函数的特点是检测到某些无法与格式控制说明匹配的情况时将scanf函数终止
第一种情况:
当你输入 6 2 时 6 被(scanf("%d", &sides) == 1 && sides > 0) 判断 6符合scanf函数返还值1 进入循环体
然后  2被 scanf("%d", &dice)判断符合输入条件被输入。

第二种情况
输入 6 a 时 6被 (scanf("%d", &sides) == 1 && sides > 0) 判断  6 符合scanf函数返还值1 进入循环体
然后 a遇到了 scanf("%d", &dice)不符合输入条件 被scanf忽略了
然后进入了第2次循环 就是 a 被 (scanf("%d", &sides) == 1 && sides > 0) 判断 不符合输入条件 返还值是 0退出循环。

这也是我刚刚想明白的地方 不对的地方请大家指正

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-9-25 21:32:38 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
        int roll_count;
       
        int roll_n_dice(int dice, int sides);
       
        int dice,roll;
        int sides;
       
       
        srand((unsigned int) time(0));   //这里是不是与后面的rand想对应
        printf("Enter the number of sides per die, 0 to stop.\n");
        while (scanf("%d", &sides) == 1 && sides > 0)
        {
                printf("How many dice?\n");
                if(scanf("%d", &dice) != 1)
                {
                        printf ("输入错误\n");
                        break;
                }
                roll = roll_n_dice(dice, sides);
                printf("You have rolled a %d using %d %d-sided dice.\n",
                        roll, dice, sides);
                printf("How many sides? Enter 0 to stop.\n");
        }
        printf("The rollem() function was called %d times.\n",
                roll_count);
       
        printf("GOOD FORTUNE TO YOU!\n");
       
        return 0;
}
int roll_count = 0;

static int rollem(int sides)
{
        int roll;
       
        roll = rand() % sides + 1;
        ++roll_count;
       
        return roll;
}

int roll_n_dice(int dice, int sides)
{
        int d;
        int total = 0;
        if (sides < 2)
        {
                printf("Need at least 2 sides.\n");
                return -2;
        }
        if (dice < 1)
        {
                printf("Need at least 1 die.\n");
                return -1;
        }
       
        for (d = 0; d < dice; d++)
                total += rollem(sides);
       
        return total;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-9-26 00:30:29 | 显示全部楼层
贝壳 发表于 2012-9-24 14:47
如果在循环里加入过滤字符和字符串的条件,程序应该就正常了。

顺便问一下,这个程序啥意思啊,输入一个筛子 6面,然后输入 3  是什么意思 计算的是什么  晕 看不明白
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-9-26 00:31:00 | 显示全部楼层
无尽的夜 发表于 2012-9-25 21:32
#include
#include  
#include  

这个程序啥意思啊,输入一个筛子 6面,然后输入 3  是什么意思 计算的是什么  晕 看不明白
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-15 05:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表