C ++测试随机数

//输出0-15的数字
#include <iostream> 
#include <string> 
using namespace std;
//更好的办法是使用数组来存储  方便索引 循环
void test01()
{
    srand(time(0));
    int t0 = 0;
    int t1 = 0;
    int t2 = 0;
    int t3 = 0;
    int t4 = 0;
    int t5 = 0;
    int t6 = 0;
    int t7 = 0;
    int t8 = 0;
    int t9 = 0;
    int t10 = 0;
    int t11 = 0;
    int t12 = 0;
    int t13 = 0;
    int t14 = 0;
    int t15 = 0;
    for (size_t i = 0; i < 500000000; i++)
    {
        int a = rand() % 16;
        if (a == 0)
        {
            t0++;
        }
        if (a == 1)
        {
            t1++;
        }
        if (a == 2)
        {
            t2++;

        }
        if (a == 3)
        {
            t3++;

        }
        if (a == 4)
        {
            t4++;
        }
        if (a == 5)
        {
            t5++;
        }
        if (a == 6)
        {
            t6++;
        }
        if (a == 7)
        {
            t7++;
        }
        if (a == 8)
        {
            t8++;
        }
        if (a == 9)
        {
            t9++;
        }
        if (a == 10)
        {
            t10++;
        }
        if (a == 11)
        {
            t11++;
        }
        if (a == 12)
        {
            t12++;
        }
        if (a == 13)
        {
            t13++;
        }
        if (a == 14)
        {
            t14++;
        }
        if (a == 15)
        {
            t15++;
        }
    }
    cout << t0 << " "<<t1 << " " << t2 << " " << t3 << " " << t4 << " " << t5 << " " << t6 << " " << t7 << " " << t8 << " " << t9 << " " << t10 << " " << t11 << " " << t12 << " " << t13 << " " << t14 << " " << t15;
}

int main() 
{
    test01();
    return 0;
}

输出结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值