35541

Why are my 'randomly' generated numbers are increasing every time I run the program?

Question:

I'm trying to get randomly generated numbers by using srand() and rand() but with no avale: every time I run the program it increases by a certain amount of numbers. But if I use a for statement, no pattern can be seen.

For example, if I use the code provided below and run and close the program 10 times, the output will be:

42 52 72 78 85 92 12 (it has reset) 32 48

<strong>Note</strong>: one strange thing I noticed about this that when I unfocus or minimize Visual Studio and close the Command Prompt, the next time I run the program the number increases by more than 20, but, if I don't unfocus or minimze Visual Studio the number increases by just little over 1-5. Why's that?

#include "stdafx.h" #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand (time(NULL)); int random_number = rand () % 100 + 1; cout << "Random number: " << random_number << endl; return 0; }

But, if I use this code:

#include "stdafx.h" #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand (time(NULL)); for (int i = 0; i < 10; i++) { int random_number = rand () % 100 + 1; cout << "Random number: " << random_number << endl; } return 0; }

the numbers don't have a clear pattern and I get the output:

31 10 81 66 74 14 6 97 39 23

They increase and decrease randomly by random amounts. What seems to be the problem here?

Answer1:

The first few random numbers returned by some versions of rand (looking at you Microsoft) are highly correlated to the seed they start with, just due to the random number generator formula being used. Since the time isn't changing much between runs, neither do the random numbers. If you throw away the first few random numbers that get returned you can get much better results.

A better solution is to use <a href="http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution" rel="nofollow">std::uniform_int_distribution</a> instead of rand.

Answer2:

Another potential issue is some implementations of rand may interact poorly with certain modulos. For that reason you may want to consider something like this instead:

int v = ((double)rand() / (double)RAND_MAX) * 100 + 1;

Still not ideal, but easy.

Recommend

  • Blackberry UI showing images at bottom
  • HTML Form That Doesn't Actually Submit Anything To The Server (Client-Side Processing in JavaSc
  • error C2099: initializer is not a constant
  • Run multiple web applications in one Visual Studio development server?
  • Unable to deploy on Windows 10 Mobile
  • Django-ckeditor not displaying correctly in html
  • mfc - MessageBox with rich text
  • Is it possible to access raw iphone audio output?
  • Wpf RichTextBox wrapping problems
  • How to get a Windows Forms panel as a bitmap with C#?
  • How do I clear the screen in C? [duplicate]
  • Geo Fix not working in Android SDK 2.2
  • mysql table locked after php crashes
  • What dll is needed for Windows.Devices.Geolocation?
  • not able to create VC++ project, with VS11
  • Visual studio 2015 keystroke with mouse button
  • c++ search a vector for element first seen position
  • How do I translate LR(1) Parse into a Abstract syntax tree?
  • ASP.NET windows authentication should always ask for credentials
  • TFS - how do I sum child task hours to parent
  • Deploying a CodeRush plugin from the Community Site
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • Hide buttons on title bar in Java
  • How to pass solution folder as parameter in command line arguments (for debug)?
  • Is it possible to define rest argument in OCaml?
  • Git describe fails to return most recent annotated tag
  • ASP.NET MVC 2 Preview 2 - display directory list rather than home/index
  • Eliminate partial duplicate rows from result set
  • Is it possible to access block's scope in method?
  • Is there a javascript serializer for JSON.Net?
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • Where to put my custom functions in Wordpress?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Apache 2.4 - remove | delete | uninstall
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Buffer size for converting unsigned long to string
  • Java static initializers and reflection
  • Binding checkboxes to object values in AngularJs