My rand() isn't very random. As a matter of fact, it's not random at all! I am running Ubuntu 10.04 on both home comp and laptop. When I execute the code:

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	int x;
	x = rand();
	cout << x;
	
	return 0;
}

I get the same result every time on both computers: 180428383

Umm... Any ideas?

You could try to specify boudaries to the rand (), maybe that would help...

Try this:

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	int x;
	x = rand() % 1500 + 1;
	cout << x;
	
	return 0;
}

>> My rand() isn't very random. As a matter of fact, it's not random at all!
>> I get the same result every time on both computers: 180428383

This is by design, you want to Initialize random number generator.

So the "% x" is required? I only know about rand() because of a tutorial I am reading. The author used rand() to create a numbers guessing game, but did not give it parameters. Thanks for the help guys.

So the "% x" is required? I only know about rand() because of a tutorial I am reading. The author used rand() to create a numbers guessing game, but did not give it parameters. Thanks for the help guys.

No. This statement: x = rand() % 1500 + 1; is a method of creating random numbers within a specified range. It is not a method of providing parameters to rand(). If you use that, your end results won't change. The only difference will be that the number displayed falls into the specified range, it still won't be "random".

"%" is the modulus operator. It has nothing to do with random number generation. It is frequently used in conjunction with the output from a RNG, but it is not actually part of the RNG system.

To make the results "random" you need to initialize the RNG as mitrmkar mentioned.

well just thought I might just help try doing this:
It gives you a random number each time

#include <iostream>
#include <cstdlib> //for the rand and srand function
#include <ctime> //for the time function

using namespace std;

int main()
{
      unsigned seed = time(0);
      srand(seed);
	
	cout << seed << endl;
	



}

just another way to do it...but It isn't implemented for a predefined range a.t.m

well just thought I might just help try doing this:
It gives you a random number each time

#include <iostream>
#include <cstdlib> //for the rand and srand function
#include <ctime> //for the time function

using namespace std;

int main()
{
      unsigned seed = time(0);
      srand(seed);
	
	cout << seed << endl;
	



}

just another way to do it...but It isn't implemented for a predefined range a.t.m

Partially true. The number that outputs is actually a timestamp, not a "random number". If you put that in a loop, the output will be either multiple copies of the same number or multiple copies of sequential numbers depending on how long and fast the program runs. The reason it's different each time the program is run is it's a numeric representation of the current time in seconds and time doesn't stand still.

The proper use of the RNG is as follows:

#include <iostream>              //I/O operations
#include <cstdlib>               //srand(), rand(), etc...
#include <ctime>                 //time(), etc...

const int setSize = 10;

using namespace std;

int main() {
  srand((unsigned) time(0));     // seed/initialize the RNG

  for (int i = 0; i < setSize; ++i) {
    int someNum = rand();         //generate a random number
    cout << someNum << " ";       //display the random number
  }

  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.