Search code examples
c++c++11randomvisual-studio-2013mersenne-twister

Can't convert uniform_int_distribution<int> to int


I'm trying to make random level generator that uses mersenne twister. Here's the code (it's just beginning, so it doesn't make much sense):

Generator.h:

//Level generator

#pragma once

class GameMap
{
public:


    static int bgmap[256][256];
    static int fg1map[256][256];
    static int fg2map[256][256];
    static int genposx, genposy, width, height;


    static std::mt19937 twister(int);
    static std::uniform_int_distribution<int> dist1(int, int);
    static std::uniform_int_distribution<int> dist2(int, int);
    static std::random_device rd;
    void Generate(int sizex, int sizey, int seed);

    GameMap();
    ~GameMap();
};

Generator.cpp:

//#include <SFML/Graphics.hpp>
#include <random>
#include "Generator.h"

GameMap::GameMap()
{
    dist1(1, 8);
    dist2(1, 248);
}


void GameMap::Generate(int sizex, int sizey, int seed = rd())
{
    twister(seed);

    genposx = 1;
    genposy = 1;

    do
    {
        genposx = dist2(twister);
        genposy = dist2(twister);
        width = dist1(twister);
        height = dist1(twister);

    } while (whatever);
}

The problem is that I can't convert uniform_int_distrubution to int. I get Intellisense error messages:

no suitable conversion from unifgorm_int_distribution<int>" to "int" exists

argument of type "std::mt19937 (*)(int)" is incompatible with parameter of type "int"

too few arguments in function call

All of those are on these lines:

genposx = dist2(twister);
genposy = dist2(twister);
width = dist1(twister);
height = dist1(twister);

I've lost many hours by searching in web an answer, but I couldn't find anything. Please help.


Solution

  • I think your main problem is declaring your class members as functions. I made some suggested changes to your code with brief explanations:

    class GameMap
    {
    public:
    
        // are these really meant to be static (one for all GameMap's)
        // or do you want one set of map data for every GameMap object (non static)? 
        /*static*/ int bgmap[256][256];
        /*static*/ int fg1map[256][256];
        /*static*/ int fg2map[256][256];
        /*static*/ int genposx, genposy, width, height;
    
    
        // static std::mt19937 twister(int); // function declaration?
        std::mt19937 twister;
    
        // probably you don't want static function declarations
        std::uniform_int_distribution<int> dist1; //(int, int);
        std::uniform_int_distribution<int> dist2; //(int, int);
        static std::random_device rd;
        void Generate(int sizex, int sizey, int seed);
    
        GameMap();
        ~GameMap();
    };
    
    GameMap::GameMap()
    : dist1(1, 8)
    , dist2(1, 248) // initialize these here
    {
    }
    
    
    void GameMap::Generate(int sizex, int sizey, int seed = rd())
    {
        twister.seed(seed); // probably you meant this?
    
        genposx = 1;
        genposy = 1;
    
        do
        {
            genposx = dist2(twister);
            genposy = dist2(twister);
            width = dist1(twister);
            height = dist1(twister);
    
        } while (whatever);
    }