首页 > 其他 > 详细

C的随机数用法

时间:2019-01-30 15:38:07      阅读:153      评论:0      收藏:0      [点我收藏+]

rand()

#include <stdio.h>
#include <stdlib.h>
int main()
{
    for (int i = 0; i < 10; i++)
    {
        printf("%d\n",rand());
    }
    system("pause");
    return EXIT_SUCCESS;
}

技术分享图片

#include <stdio.h>
#include <stdlib.h>
int main()
{
    for (int i = 0; i < 10; i++)
    {
        printf("%d\n",rand()%10);
    }
    system("pause");
    return EXIT_SUCCESS;
}

技术分享图片

 指定随机数的生成范围例如生成8以内的随机数---只不过....程序每次运行时都是生成这几个货

#include <stdio.h>
#include <stdlib.h>
int main()
{
    srand(8);
    for (int i = 0; i < 10; i++)
    {
        printf("%d\n",rand()%10);
    }
    system("pause");
    return EXIT_SUCCESS;
}

技术分享图片

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand((unsigned)time(NULL));
for (int i = 0; i < 10; i++)
{
printf("%d\n",rand()%100+21);
}
system("pause");
return EXIT_SUCCESS;
}

技术分享图片技术分享图片技术分享图片

 

C的随机数用法

原文:https://www.cnblogs.com/saintdingspage/p/10337975.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!