user11307761
user11307761

Reputation:

How to generate multiple random numbers?

At the moment, this program generates only one random number printed 3 times. How can I get it to print three different random numbers?

Also I'm surprised I didn't need the ctime header even though I used time(0).

//This program generates three random numbers.

#include <iostream>
#include <cstdlib>
using std::endl;
using std::cout;

int main() {
    unsigned number;

    srand(time(0));
    number = rand() % 10 + 1;

    cout << number << "         ";
    cout << number << "         ";
    cout << number << endl;
    return 0;
}

Upvotes: 2

Views: 1400

Answers (4)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

At the moment, this program generates only one random number printed 3 times. How can I get it to print three different random numbers?

You seem to have a heavy misconception what

number = rand() % 10 + 1;

really does.

At the point you do that assignment

rand() % 10 + 1

the value of number is evaluated and stored once to the variable from that mentioned line above.

Further accesses to number won't trigger that evaluation anymore.


If you want to do the evaluation everytime you access a specific "variable", you might want to use a lambda function instead:

#include <cstdlib>
#include <ctime>
#include <iostream>

int main() {

    std::srand(std::time(nullptr));
    auto number = []() {
        return std::rand() % 10 + 1;
    };

    std::cout << number() << "         ";
    std::cout << number() << "         ";
    std::cout << number() << std::endl;
}

Here's a live demo


Also I'm surprised I didn't need the ctime header even though I used time(0).

It happens with specific compiler implementations that you're lucky and one of the already used standard headers already includes the required ctime header for the time(0) call.

The safe and portable way you should rather write

#include <ctime>

// ...
    std::time(nullptr)
// ...

Upvotes: 3

dbush
dbush

Reputation: 223739

Assignments in C++ are not formulas that are applied every time the variable is used. So when you do this:

number = rand() % 10 + 1;

You assign a value to number once, at the point in the program where it appears.

If you want to get more random numbers, you need to call rand and assign to number multiple times.

number = rand() % 10 + 1;
cout << number << "         ";
number = rand() % 10 + 1;
cout << number << "         ";
number = rand() % 10 + 1;
cout << number << endl;

Upvotes: 1

Saumitra Topinkatti
Saumitra Topinkatti

Reputation: 68

Solution:

#include <iostream>
#include <cstdlib>
using std::endl;
using std::cout;

int main()

{
  unsigned number;

  srand(time(0));
  number = rand() % 10 + 1;
  cout << number << "         ";
  number = rand() % 10 + 1;
  cout << number << "         ";
  number = rand() % 10 + 1;
  cout << number << endl;
  return 0;
}

The reason why your code snippet returned the same random three times, is,

//This program generates three random numbers.

#include <iostream>
#include <cstdlib>
using std::endl;
using std::cout;

int main()

{
  unsigned number;

  srand(time(0));
  number = rand() % 10 + 1;

  cout << number << "         ";
  cout << number << "         ";
  cout << number << endl;
  return 0;
}

Value of 'Random' is assigned to number only once, and it will remain constant until it is assigned again.

Hope you got me right ;)

Upvotes: 0

Hoog
Hoog

Reputation: 2298

You only set number once, it will continue to be the random number you have assigned to it until you assign something else to it, such as another random number from rand()

/This program generates three random numbers.

#include <iostream>
#include <cstdlib>
using std::endl;
using std::cout;

int main()

{
  unsigned number;

  srand(time(0));
  number = rand() % 10 + 1;  
  cout << number << "         ";

  number = rand() % 10 + 1;
  cout << number << "         ";

  number = rand() % 10 + 1;
  cout << number << endl;
  return 0;
}

Upvotes: 2

Related Questions