关于随机函数的问题,求教。

Camel_J 2018-01-01 11:31:57
extern int roll_count;

int roll_n_dice(int dice, int sides);

#include"diceroll.h"
#include<stdio.h>
#include<stdlib.h>

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 -1;
}
if(dice < 1)
{
printf("Need at least 1 die.\n");
return -1;
}
for(d = 0 ; d < dice ; d++)
total += rollem(sides);

return total;
}

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"diceroll.h"

int main(void)
{
int dice, roll;
int sides;
int status;

srand((unsigned int)time(0)); // 请问这里的srand()是否没什么用,如果有用,在这个程序什么地方用到?如何使用?
printf("Enter the number or sides per die,0 to stop.\n");
while(scanf("%d",&sides) == 1 && sides > 0)
{
printf("How many dice?\n");
if(status = scanf("%d",&dice) != 1)
{
if(status == EOF)
break;
else
{
printf("You should have entered an integer.");
printf(" Let's begin again.\n");
while(getchar() != '\n')
continue;
printf("How many sides?Enter 0 to stop.\n");
continue;
}
}
roll = roll_n_dice(dice,sides);
printf("You have rolled a %d using %d %d-sides 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;
}
...全文
284 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-01-03
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\rand.c
/***
*rand.c - random number generator
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines rand(), srand() - random number generator
*
*******************************************************************************/

#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>

/***
*void srand(seed) - seed the random number generator
*
*Purpose:
*       Seeds the random number generator with the int given.  Adapted from the
*       BASIC random number generator.
*
*Entry:
*       unsigned seed - seed to seed rand # generator with
*
*Exit:
*       None.
*
*Exceptions:
*
*******************************************************************************/

void __cdecl srand (
        unsigned int seed
        )
{
        _getptd()->_holdrand = (unsigned long)seed;
}


/***
*int rand() - returns a random number
*
*Purpose:
*       returns a pseudo-random number 0 through 32767.
*
*Entry:
*       None.
*
*Exit:
*       Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/

int __cdecl rand (
        void
        )
{
        _ptiddata ptd = _getptd();

        return( ((ptd->_holdrand = ptd->_holdrand * 214013L
            + 2531011L) >> 16) & 0x7fff );
}
mstlq 2018-01-01
  • 打赏
  • 举报
回复
http://zh.cppreference.com/w/cpp/numeric/random/rand http://zh.cppreference.com/w/cpp/numeric/random/srand 请将上面两个链接的内容完整地读一下,并注意这句话——每次以同一 seed 播种 rand() 时,它必须产生相同的值数列。
paschen 2018-01-01
  • 打赏
  • 举报
回复
srand只是初始化随机数种子,一般运行一次就可以了,可以在程序开始时运行,或者第一次需要随机数的时候运行 http://en.cppreference.com/w/c/numeric/random/srand

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧