使用Rand()在C ++中获取一个8位数的随机数 [英] Using Rand() to get an 8 digit random number in C++

查看:469
本文介绍了使用Rand()在C ++中获取一个8位数的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么这不起作用,它的编译但不给我结果..阅读除了couts之外的评论,看看它的作用。我真的尝试了一切,没有任何运气。



Could someone please explain to me why this is not working, its compiling but not giving me the results.. read the comments besides the couts to see what its doing. I have literally tried everything and not having any luck whatsoever.

    int i;
    srand (time(NULL));
    i = (rand() % 99999999 + 1);
cout << i << endl; //This cout displays a 5 digit number that counts upwards every time
i = 0;
    while (i < 99999999)
  {
          i = (rand() % 99999999 + 1);
  }
cout << i << endl; //This one stalls my program

推荐答案

它可能取决于您使用的系统:如果您的整数值是16位,那么它永远不会生成8位数,因为它可以容纳的最大值是2 ^ 15或32K。



我怀疑你的程序没有被cout停滞,但是从不退出while循环因为每个生成的值都小于99999999 + 1 ...





有没有办法解决这个问题?我应该使用不同的变量





如果您使用的是16位整数的系统,则no - rand返回一个整数,因此它可以不会给你更大的数字 - 32K。



然而,没有什么可以阻止你把它叫做两次长:



It may depend on the system you are using: if your integer value is 16 bits, then it can never generate an 8 digit number because the largest value it can hold is 2^15 or 32K.

I suspect that your program isn''t "stalled" by the cout, but never exits the while loop because every value generated is less than 99999999 + 1...


"is there a way around this?? should I be using a different variable"


If you are on a system with a 16 bit integer, then no - rand returns an integer, so it can''t give you bigger numbers rthan 32K.

However, there is nothing stopping you from calling it twice into a long:

long FourDigitRandom()
   {
   return (long) rand() % (9999 + 1); 
   }
...
   long l = FourDigitRandom() + (FourDigitRandom() * 10000);
...


您需要更多地考虑您的代码!

1.rand()返回0和RAND_MAX(32767)之间的伪随机(即任意数字)。除非您通过零填充将值格式化为8位,否则输出可以是1到5位的任何长度。

2.使用mod 99999999意味着您最终得到的值与rand返回的值相同,因为任何值都会小于那个,所以模数(余数)总是那个值。

3.你的while循环永远不会完成,因为''我'永远不会是99999999。



要使用rand从两个值之间获取伪随机值,您需要这样做:



You need to think more about your code!
1. "rand()" returns a pseudo-random (i.e. any number) between 0 and RAND_MAX (32767). Unless you format the value to 8 digits by zero padding, the output could be any length from 1 to 5 digits.
2. Using mod 99999999 means that you end up with the same value that rand returned, because any value will be less than that, so the modulus (remainder) is always that value.
3. Your while loop is never going to finish, because ''i'' can never be 99999999.

To get a pseudo-random value from between two values using rand, you will need to do this:

int value = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
            + range_min;





,如下例 rand() [ ^ ]



在您的情况下,range_max = 99999999和range_min = 1.



或者,如果你想确保它总是8位数而没有前导零,那么range_min = 10000000.



问候,

Ian。



as given in the example here rand()[^]

In your case, range_max = 99999999 and range_min = 1.

Or, if you want to ensure it is always 8 digits without leading zeros, then range_min = 10000000.

Regards,
Ian.


你的rand()所做的是给出0到32767之间的数字。现在,如果你想让你的数字总是一个8位数,不小于1000000,您可以使用的有效解决方案是分别生成每个数字。此外,如果您可以存储8位数字,这取决于您的机器。所以,如果你只想显示,我建议使用两个4位数字。



这是你可以使用的:



What your rand() does is give a number between 0 and 32767. Now, if you want your number to be ALWAYS a 8 digit number, not smaller than 1000000, what you can use as an effective solution is generate each number separately. Also, it depends on your machine if you can store 8 digit numbers. So, if you only want to display, I''d suggest using two 4 digit numbers.

Here''s what you can use :

int i = 0,j = 0;

i = rand() % 9 + 1;            //First number should not be 0.

for (int k = 1; k < 4; k++) {
    i = i*10 + rand()%10;      //Generate the next 3 digits.
}

for (int k = 0; k < 4; k++) {
    j = j*10 + rand()%10;      //Generate the 4 digits of the lower part.
}

cout<<"Random number : "<<i<<j;

// If you want the 8 digit number, save in long int
long int res = i*1000 + j;
cout<<res;


这篇关于使用Rand()在C ++中获取一个8位数的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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