简体   繁体   English

如何生成多个随机数?

[英]How to generate multiple random numbers?

At the moment, this program generates only one random number printed 3 times. 目前,该程序仅生成一次打印3次的随机数。 How can I get it to print three different random numbers? 如何让它打印三个不同的随机数?

Also I'm surprised I didn't need the ctime header even though I used time(0). 此外,我很惊讶即使我使用时间(0)我也不需要ctime标头。

//This program generates three random numbers.

#include <iostream>
#include <cstdlib>
using std::endl;
using std::cout;

int main() {
    unsigned number;

    srand(time(0));
    number = rand() % 10 + 1;

    cout << number << "         ";
    cout << number << "         ";
    cout << number << endl;
    return 0;
}

At the moment, this program generates only one random number printed 3 times. 目前,该程序仅生成一次打印3次的随机数。 How can I get it to print three different random numbers? 如何让它打印三个不同的随机数?

You seem to have a heavy misconception what 你似乎有一种沉重的误解是什么

 number = rand() % 10 + 1; 

really does. 真的。

At the point you do that assignment 在你执行该任务时

 rand() % 10 + 1 

the value of number is evaluated and stored once to the variable from that mentioned line above. 的值number进行评价,并从上面提到行的变量存储一次

Further accesses to number won't trigger that evaluation anymore. 进一步访问number将不再触发该评估。


If you want to do the evaluation everytime you access a specific "variable" , you might want to use a lambda function instead: 如果要在每次访问特定“变量”时进行评估,可能需要使用lambda函数

#include <cstdlib>
#include <ctime>
#include <iostream>

int main() {

    std::srand(std::time(nullptr));
    auto number = []() {
        return std::rand() % 10 + 1;
    };

    std::cout << number() << "         ";
    std::cout << number() << "         ";
    std::cout << number() << std::endl;
}

Here's a live demo 这是一个现场演示


Also I'm surprised I didn't need the ctime header even though I used time(0) . 此外,我很惊讶即使我使用time(0)我也不需要ctime标头。

It happens with specific compiler implementations that you're lucky and one of the already used standard headers already includes the required ctime header for the time(0) call. 在特定的编译器实现中,您很幸运,并且已经使用过的标准头之一已经包含了time(0)调用所需的ctime头。

The safe and portable way you should rather write 你宁愿写的安全便携的方式

#include <ctime>

// ...
    std::time(nullptr)
// ...

You only set number once, it will continue to be the random number you have assigned to it until you assign something else to it, such as another random number from rand() 您只需设置一次number ,它将继续为您分配给它的随机数,直到您为其指定其他内容,例如来自rand()另一个随机数字

/This program generates three random numbers.

#include <iostream>
#include <cstdlib>
using std::endl;
using std::cout;

int main()

{
  unsigned number;

  srand(time(0));
  number = rand() % 10 + 1;  
  cout << number << "         ";

  number = rand() % 10 + 1;
  cout << number << "         ";

  number = rand() % 10 + 1;
  cout << number << endl;
  return 0;
}

Assignments in C++ are not formulas that are applied every time the variable is used. C ++中的赋值不是每次使用变量时都应用的公式。 So when you do this: 所以当你这样做时:

number = rand() % 10 + 1;

You assign a value to number once , at the point in the program where it appears. 您可以在程序出现的位置为number赋值一次

If you want to get more random numbers, you need to call rand and assign to number multiple times. 如果您想获得更多随机数,则需要调用rand并多次分配给number

number = rand() % 10 + 1;
cout << number << "         ";
number = rand() % 10 + 1;
cout << number << "         ";
number = rand() % 10 + 1;
cout << number << endl;

Solution: 解:

#include <iostream>
#include <cstdlib>
using std::endl;
using std::cout;

int main()

{
  unsigned number;

  srand(time(0));
  number = rand() % 10 + 1;
  cout << number << "         ";
  number = rand() % 10 + 1;
  cout << number << "         ";
  number = rand() % 10 + 1;
  cout << number << endl;
  return 0;
}

The reason why your code snippet returned the same random three times, is, 您的代码段返回相同的随机三次的原因是,

//This program generates three random numbers.

#include <iostream>
#include <cstdlib>
using std::endl;
using std::cout;

int main()

{
  unsigned number;

  srand(time(0));
  number = rand() % 10 + 1;

  cout << number << "         ";
  cout << number << "         ";
  cout << number << endl;
  return 0;
}

Value of 'Random' is assigned to number only once, and it will remain constant until it is assigned again. “随机”的值仅分配给数字一次,并且在再次分配之前它将保持不变。

Hope you got me right ;) 希望你让我正确;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM