0

I was trying the generate random numbers using mt19937 in c++ as shown in the code but I encounterd some weired behaviour.

#include <iostream>
#include <random>
#include <chrono>
using namespace std;

int main() {
    mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
    int n = rng() % 10 + 2;
    cout << n << endl;
    for (int i = 0; i < n; i++) {
        int x = rng() % 10 + 1;
        int y = rng() % 10 + 1;
        cout << x << " " << y << " " << endl;
    }
}

When I run my code in "CLion", It runs and behaves as expected;however, when trying to run the code using the terminal, It doesn't compile and I recieve 'mt19937' was not declared in this scope as an error message. Here is the Error message screenshot

Update 1

I removed the #include <bits/stdc++. h> from the code but the code still doesn't compile from terminal

Update 2

I have been using Mingw version which comes with Codeblocks so I uninstalled it and redownloaded Mingw from SourceForge. Surprisingly, the problem is now solved and I could run the code from the terminal.

Update 3

Now when I compile some program using the command g++ <name>.cpp , a file named a.exe is created. I don't know why its name is always a.exe regardless of the name of the original cpp file. In order to name the .exe file the same as the cpp file, I have to write g++ <name>.cpp -o <name>. Can anyone help explain why the name is always a.cpp ??

Peter O.
  • 32,158
  • 14
  • 82
  • 96
LIO
  • 27
  • 4
  • What version of GCC are you using when building from the terminal? – Some programmer dude Sep 13 '20 at 21:58
  • 2
    Did you try including the correct header ``? – Quimby Sep 13 '20 at 22:00
  • 4
    Also please copy-paste text *as text* into your question, including build errors and console input/output. And please [don't include ``](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) – Some programmer dude Sep 13 '20 at 22:01
  • 6
    `#include using namespace std;` is a terrible habit, no wonder you are running into trouble. – Paul Sanders Sep 13 '20 at 22:01
  • My gcc version is `(i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0` . I tried to remve `` and include the necessary libraries but the problem didn't change – LIO Sep 13 '20 at 22:06
  • 1
    Please update the question with your last version that still doesn't work. – rustyx Sep 13 '20 at 22:16
  • 1
    Cannot reproduce. Add the full build commend you're using to the question and type `g++ -v` on the command line to be absolutely certain you don't have an older g++ lurking earlier in the path. – user4581301 Sep 13 '20 at 22:20
  • 1
    See the question/answer here: https://stackoverflow.com/questions/4980819/what-are-the-gcc-default-include-directories for a method to list g++'s default include directories. See https://stackoverflow.com/questions/44734397/which-c-standard-is-the-default-when-compiling-with-g for a question/answer on how to see which default c++ version your compiler uses. Please tell us both. – JohnFilleau Sep 13 '20 at 22:21
  • "Update 3" is a totally different and separate question. It is the default behavior though, and is well-documented. – Some programmer dude Sep 14 '20 at 00:04

0 Answers0