简体   繁体   English

C++ 从函数返回 (uniform_int_distribution)

[英]C++ Return (uniform_int_distribution) from a function

I made this random class so I can use random numbers in my game engine whenever I need, but I'm having a problem with the functions, How I'm I suppose to return R ?我创建了这个随机类,这样我就可以在需要时在游戏引擎中使用随机数,但是我遇到了函数问题,我想如何返回 R ? (please keep in mind that I'm new to coding in general) (请记住,我一般不熟悉编码)

#pragma once
#include <ctime>
#include <random>

using namespace std;

class Random
{
private:


public:
    Random() { default_random_engine RandomNumber(time(0));};

    int RandomNumber()
    {
        uniform_int_distribution<int> R();
         return = R;
    }

    float RandomFloat()
    {
        uniform_int_distribution<float> R();
    }

    int RandomNumberRange(int Min =0,int Max =0)
    {
        uniform_int_distribution<int> R(Min,Max);
    }

    float RandomFloatRange(float Min = 0, float Max = 0)
    {
        uniform_int_distribution<float> R(Min,Max);
    }}; 

The random number engine is supposed to be reused, but currently you are declaring it as a local variable, which is never used again and destroyed when the constructor exits.随机数引擎应该被重用,但目前你将它声明为一个局部变量,当构造函数退出时它永远不会被再次使用和销毁。 Instead, make the default_random_engine a member of your class.相反,让default_random_engine成为您的类的成员。

Then, to get an actual random number from a distribution, you need to call it via its overloaded call operator and pass it the random number engine:然后,要从分布中获取实际的随机数,您需要通过其重载的调用运算符调用它并将其传递给随机数引擎:

class Random
{
private:
    default_random_engine RandomNumber{time(0)};

public:

    int RandomNumberRange(int Min =0,int Max =0)
    {
        return uniform_int_distribution<int>{Min,Max}(RandomNumber);
    }

    //...
};

There really isn't any reason to return the distribution itself.真的没有任何理由退回发行版本身。 So I interpreted your question in a way that probably makes more sense.所以我以一种可能更有意义的方式解释了你的问题。

Also note that the seeding with time(0) is probably a bad one.另请注意,使用time(0)进行播种可能很糟糕。 But this is a huge topic itself.但这本身就是一个巨大的话题。 In particular, if you happen to create multiple instances of the Random class in short succession, they will be equally seeded.特别是,如果您碰巧在短时间内连续创建Random类的多个实例,则它们将被同等播种。 Instead反而

default_random_engine RandomNumber{std::random_device{}()};

may work better.可能工作得更好。 It will probably use random numbers provided by the system.可能会使用系统提供的随机数。 (But it may not, for example older versions of MinGW did generate deterministic results this way.) (但它可能不会,例如旧版本的 MinGW 确实以这种方式生成了确定性结果。)

    int RandomNumber()
    {
        uniform_int_distribution<int> R();
        return = R;
    }

Return type and the type of R in the above function are different.返回类型与上述函数中R的类型不同。 R is of type uniform_int_distribution<int> whereas return type is just int . R是类型uniform_int_distribution<int>而返回类型只是int To return R() you have to change the function return type.要返回R()你必须改变函数的返回类型。

    uniform_int_distribution<int> RandomNumber()
    {
        uniform_int_distribution<int> R;
        return R;
    }

Note that it is not return = R .请注意,它不是return = R return is a keyword and not a variable that you can assign to. return是一个关键字,而不是一个可以赋值的变量。 To return a variable just use return R;要返回一个变量只需使用return R; . .

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

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