C++:自定义rand_int函数的参数未初始化

C++: Params for custom rand_int function not being initialized

本文关键字:参数 初始化 函数 int 自定义 rand C++      更新时间:2023-10-16

对于内联代码的长时间转储,我提前道歉,但我真的找不到任何不相关的我可以删除的东西,因为我不知道问题到底在哪里。

正在尝试创建一个以简单方式处理在某个范围内生成随机数的类,但是每当我运行代码时,我都会得到垃圾值,这可能表明某些内容未在某处正确初始化。问题是我找不到哪里。

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// calculates a random number within a range.
class Random
{
    int x, y;
    int range;
public:
    Random() : x(0), y(1), range(1) {
        srand(static_cast<unsigned int>(time(NULL)));
    }
    ~Random() {}
    // setters and getters:
    void setX(int x) {this->x = x;}
    void setY(int y) {this->y = y;}
    void setRange(void) {this->range = (this->y - this->x) + 1;}
    int getX(void) {return this->x;}
    int getY(void) {return this->y;}
    int getRange(void) {return this->range;}
    // member functions:
    // changes negative numbers to positive.
    void absXY(void) {
        if(getX() < 0) setX(getX() * -1); // changes negative to positive.
        if(getY() < 0) setY(getY() * -1);
    }
    // swaps values around if x > y.
    void swapXY(void) {
        if(getX() > getY()) { // swaps x with y.
            int tmp = getX();
            setX(getY());
            setY(tmp);
        }
    }
    // calculates rand num and returns it.
    int getRandIntFrom(int a, int b) {
        setX(a);
        setY(b);
        setRange();
        absXY();
        swapXY();
        return getX() + (rand() % getRange());
    }
};
int main(int argc, char* argv[])
{
    Random r;
    if(argc == 3) {
        cout << r.getRandIntFrom((int)argv[1], (int)argv[2]) << endl;
        cout << r.getX() << endl;
        cout << r.getY() << endl;
    }
    else {
        cout << "Invalid number of command line arguments." << endl;
        return -1;
    }
    return 0;
}

我一次又一次地查看了这段代码,但看不到问题所在。

  • 创建 Random 的对象时,PRNG 已设定种子,并在初始值设定项列表中为xyrange变量提供默认值。

  • 二传手和吸盘手是有效的,就我曾经使用过它们而言。

  • 设置和获取是在getRandIntFrom()函数中完成的。

  • 我使用值 2 和 8 运行程序,从而跳过了 absXY()swapXY() 函数中的所有代码,因此这些代码不会搞砸。

  • 计算随机数本身的程序逻辑是合理的。 rand() % x返回从 0x-1 的任何位置,因此 mod 按 1+范围rand()并添加下限。使用 2 和 8,这是:

    rand() % ((8-2)+1) ->
    rand() % 7 => 0..6 ->
    2+(rand() % 7) => 2..8
    

然而,当我运行程序时,我每次都会得到垃圾结果。

> g++ Random.cpp -o random -Wall -std=c++11
> random 2 8
5641728
5641728
5642296

这里可能出了什么问题?我认为这可能与初始值设定项列表有关,但这是我唯一能想到的。

当您读取命令行参数时,您正在将 c 字符串转换为整数。您得到的是存储命令行参数的地址。因此,您的 Random 类也会返回与此地址 + 给定范围匹配的值。代替强制转换,您可以使用atoi()将主函数更改为。

int main(int argc, char* argv[])
{
    Random r;
    if (argc == 3) {
        cout << r.getRandIntFrom(atoi(argv[1]), atoi(argv[2])) << endl;
        cout << r.getX() << endl;
        cout << r.getY() << endl;
    }
    else {
        cout << "Invalid number of command line arguments." << endl;
        return -1;
    }
    return 0;
}

编辑:

亚历克斯更快

字符串不能转换为带有强制转换的整数:

r.getRandIntFrom((int)argv[1], (int)argv[2])

改为考虑std::stoi()或类似

try
{
    cout << r.getRandIntFrom(std::stoi(argv[1]), std::stoi(argv[2]) << endl;
    cout << r.getX() << endl;
    cout << r.getY() << endl;
}
catch (std::invalid_argument&)
{
    cout << "invalid argumentn";
    return -1;
}
catch (std::out_of_range&)
{
    cout << "out of rangen";
    return -1;
}