Random numbers always giving 0

I'm having an issue trying to generate two random numbers. I've looked up tutorials and everything seems right, but I guess I'm missing something! My values always return 0. I'm not sure if it's because I'm trying to create one function that I can call multiple times (hopefully that's the correct terminology). Here is what I have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstdlib)
#include <ctime>

using namespace std;

void randomNumber();

double random;
int main(){

void randomNumber();

cout << "Random number 1: " << random << endl;
cout << "Random number 2: " << random << endl;

system("PAUSE");

return 0;
}
void randomNumber(){
    double random;

    srand(time(NULL));

    random = (1 + rand() % 50);
}


I've also tried it with <time.h> library and it still doesn't work.
"random" (line 9) is a different variable than "random" (line 22).

The line 9 variable is global, so it is automatically initialized to zero.

Call srand() only once, at the beginning of main.

Better yet, avoid rand().
(C++ is somewhat obnoxious because you must code a lot of boilerplate to use random stuff, but it is significantly better.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <chrono>
#include <random>

std::mt19937& rng()
{
  auto seed = std::chrono::system_clock::now().time_since_epoch().count();
  static std::mt19937 g( seed );
  return g;
}

double random( double min, double max )
{
  std::uniform_real_distribution <double> d( min, max );
  return d( rng() );
}

#include <iostream>
using namespace std;

int main()
{
  cout << "Random number 1: " << random( 1, 50 ) << "\n";
  cout << "Random number 2: " << random( 1, 50 ) << "\n";
}

Also, please realize that a floating point random number range is different than an integer range.

If you were to use a std::uniform_int_distribution you would get a number in [min,max].
But with a std::uniform_real_distribution you'll get a number in [min,max).

It's a subtle difference, but in practice it depends on what kind of data you really need.

Hope this helps.
The function randomNumber is not called. Ever.


Even if you would call it, it would not help you. You do store the value in variable random, which was declared on line 22. A local automatic variable. It does cease to exists when the function returns.

Your main() shows the value of variable random that was declared on line 9. A global variable. You should not use global variables without a very good reason. Functions can return a value (like the rand() does), or they can have by reference arguments.

Declaration of a function inside another function's body (like you do on line 12) should be avoided. Besides, you already did declare the function on line 7.

One more thing. You would (re)call srand() right before each call to rand(). There is no reason to call the srand() more than once in a program.

PS. The srand() and rand() have been deprecated. Learn to use <random> instead. See http://www.cplusplus.com/reference/random/
Thanks for the tips guys. Unfortunately, I have to use srand even though both of you recommended not to. I was however able to get most of it to work. I just put the function inside main and did not try to create a shortcut to it. So the new function inside main () now looks like this

1
2
3
4
5
double random;
srand(time(NULL));
random = (rand() % 50 +1;
cout << "Random 1: " << random << endl;
cout << "Random 2: " << random << endl;


This works, but it gives the same value twice. I have also tried to just write two separate random functions using random1 and random2 as my variables, but it still gives the same number twice. Any solutions on this part?

Edit: I believe I have a solution...I'll update momentarily...
Edit 2: I have it giving out two numbers now, but don't know how to put a cout before each one.

1
2
3
srand(time(NULL));
for (int x = 1; x <3; x++)
cout << 1 + (rand() %50) << endl;


Edit 3: Forgot about matrixes. I'll work on that and post a new thread if need it! Thanks guys!
Last edited on
Using srand() and rand():
http://www.cplusplus.com/faq/beginners/random-numbers/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int random_int_in_range( int first, int last )
{
  /* This function implements the method recommended by the knowing
   * folks at comp.lang.c: http://c-faq.com/lib/randrange.html
   * Returns an integer in [first, last].
   */
  unsigned int N = (last - first <= RAND_MAX)  /* Make sure the algorithm    */
                 ? (last - first + 1U)         /* terminates by keeping N    */
                 : (RAND_MAX + 1U);            /* in rand()'s maximum range. */
  unsigned int x = (RAND_MAX + 1U) / N;
  unsigned int y = x * N;
  unsigned int r;
  do {
    r = rand();
  } while (r >= y);
  return r / x + first;
}

int main()
{
  srand( time( NULL ) );
  cout << "Random 1: " << random_int_in_range( 1, 50 ) << "\n";
  cout << "Random 2: " << random_int_in_range( 1, 50 ) << "\n";
}

The point of a function is to generate a new value each time. Variables only store values.

Hope this helps.
Topic archived. No new replies allowed.