c++不能正确显示随机数

C++ Not displaying Random number correctly

本文关键字:显示 随机数 不能 c++      更新时间:2023-10-16

我想为温度生成一个随机数。我使用的代码如下:

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;
}

程序运行时,不是显示1到100之间的数字,而是显示以下内容:

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

在我的例子中,我看到十六进制58,十进制88(5*16+8)。以下是使文章完整的官方链接。

c++论坛:

http://www.cplusplus.com/forum/windows/51591/

细节解释道:http://www.cplusplus.com/reference/ios/dec/