我的随机数发生器有什么问题? [英] What's wrong with my random number generator?

查看:115
本文介绍了我的随机数发生器有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是潜入一些C ++,我决定做一个随机数生成器(如何随机数,这是真的没有关系)。大多数代码是复制的,但net但我的新手眼睛不能看到任何错误,这是有什么办法可以调整,每次给一个数字不是6每次?

I'm just diving into some C++ and I decided to make a random number generator (how random the number is, it really doesn't matter). Most of the code is copied off then net but my newbie eyes cannot see anything wrong with this, is there any way this can be tweaked to give a number other than "6" each time?

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
int random_number(int min, int max)
{
    srand((unsigned)time(0));
    int random_num;
    int range=(max-min)+1;
    random_num = min+int(range*rand()/(RAND_MAX + 1.0)); 
    return random_num;
}
int main()
{
    for(int i =0;i < 100;i++)
    {
            cout << random_number(3,10) << endl;
    }
}


推荐答案

 srand((unsigned)time(0));
  for(int i =0;i < 100;i++)
    {
    	std::cout << random_number(3,10) << endl;
    }

这篇关于我的随机数发生器有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆