1

Im having an issue regarding visual studio and the use of random. If i run the following code in the linux based Ubuntu, or even just using CMD a random number is produced everytime. However, when using visual studio code the number 3 is constantly returned in a sample size of 10 attempts. Does anyone know why this is happening or if visual studio code just doesn't support the use of random for C++? This is is definitely an issue with visual studio code.

#include <iostream>
#include <random>
int main()
{
std::random_device random_device;
std::mt19937 engine{random_device()};
std::uniform_int_distribution<int> dist(0, 6);

int random_index = dist(engine);
std::cout << "This is a random number" << random_index;

    return 0;
}
Jeff Linahan
  • 3,775
  • 5
  • 37
  • 56
David Ling
  • 145
  • 1
  • 12
  • 1
    Know the difference between a compiler and an editor. It's not an issue with VS Code. The GCC compiler that you're probably using has a deterministic `std::random_device` which is allowed by the standard. – eesiraed Apr 18 '20 at 00:18
  • 2
    Visual Studio Code is an editor. It has nothing to do with how the program generated from the code will behave. What is important to answer the question is what *compiler* you are using. – walnut Apr 18 '20 at 00:19
  • You could create a `seed_seq` where you mix in some entropy of your own and use that for seeding. I made an example of how it could be done and it's the last bullet [here](https://stackoverflow.com/a/60723456/7582247) – Ted Lyngmo Apr 18 '20 at 00:31
  • This returns different numbers for me in MSVC. – Jeff Linahan Apr 18 '20 at 02:07
  • @walnut im using mingw – David Ling Apr 18 '20 at 02:11
  • @BessieTheCow should i change compiles? – David Ling Apr 18 '20 at 02:17

0 Answers0