C++ C++;未正确显示随机数
我想为温度生成一个随机数。我使用的代码如下:C++ C++;未正确显示随机数,c++,visual-studio-2010,visual-c++,C++,Visual Studio 2010,Visual C++,我想为温度生成一个随机数。我使用的代码如下: int Temp() { // genreate random temperture // initialize random seed: srand ( time (NULL) ); // generate number between 1 and 100: int t = rand() % 100 + 1; std::cout << t << std::endl;
int Temp()
{
// genreate random temperture
// initialize random seed:
srand ( time (NULL) );
// generate number between 1 and 100:
int t = rand() % 100 + 1;
std::cout << t << std::endl;
return t;
}
有人能解释一下哪里出了问题或者为什么出了问题吗
编辑:如果有人怀疑我使用了以下内容:
010C1109
#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <map>
#include <cstdlib>
#include <cstring>
#pragma once
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#布拉格语一次
如何选择数字的显示方式:
std::cout << std::hex << t << std::endl; //displays in hexadecimal
std::cout << std::dec << t << std::endl; //displays in decimal
std::cout可能您之前将std::hex
传递给了std::cout
。忘了提到我正在调用函数。只是因为某些原因,它没有正确显示。我没有使用过std::hex,我以前从未使用过它。你能生产SSCCE吗?这与问题无关,但应在程序启动时调用一次srand
,而不是在每次调用rand
。使用提供的代码(请参阅)的最简单程序不会在GCC或Visual Studio中输出类似的内容。您是否可能正在执行类似于std::cout的操作?OP给出的示例是十六进制的,但它比int t=rand()%100+1
生成的要大得多(0x010C1109
=17568009
),除非发生其他意外情况。在我看来更像是一个记忆地址。还有,某人的。这个