-2
int array[5];
int Random;
for (int i = 0; i <5; i++)
{
    cin>>array[i];
}
for (int j = 0; j < 5; j++)
{
    Random = array[rand() % array[j]];
}
cout << Random << endl;

This is giving me continously return 1 but i want different number every time

mrflash818
  • 930
  • 13
  • 24
  • 4
    [Are you sure it's not random?](http://dilbert.com/strip/2001-10-25) – scohe001 Sep 21 '18 at 14:34
  • 3
    Possible duplicate of [Why does rand() yield the same sequence of numbers on every run?](https://stackoverflow.com/questions/9459035/why-does-rand-yield-the-same-sequence-of-numbers-on-every-run) – scohe001 Sep 21 '18 at 14:35
  • 1
    Please edit your question to contain [mcve] – Slava Sep 21 '18 at 14:37
  • 3
    `array[rand() % array[j]]` looks like a very easy place to go out of the bounds of the array. – NathanOliver Sep 21 '18 at 14:38
  • 1
    @scohe001 What about the [Randal Number Generator](https://xkcd.com/221/) – user4581301 Sep 21 '18 at 14:55
  • @user4581301 xkcd already has so much exposure on the SE sites, we need more Dilbert! :P – scohe001 Sep 21 '18 at 14:56
  • Your code example would be much easier for folks to understand if it simply initialized `array` with fixed values instead of reading in unspecified values from the console. Sure, that's the way you wrote the code, but you should change it to make it easier for people here to see what the problem is. – Pete Becker Sep 21 '18 at 15:23
  • without a different seed random is always starts from same position. This is very helpful in testing. – kelalaka Sep 21 '18 at 16:37

2 Answers2

2

Rand is basically obsolete.
There were just so many complaints about how bad it was (because to use it correctly you had to remember to do a couple of things). Even Peris in his answer does not correct for uneven ranges.

So please try and use the modern random library it is much more powerful. Though its documentation is hard going you don't need to read it all. Here is a simple example of how to use it.

#include <random>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int>  array(5, 0);
    for (int i = 0; i < 5; ++i)
    {
        std::cin >> array[i];
    }

    std::random_device rd;
    std::mt19937       gen(rd());
    std::uniform_int_distribution<> dis(0, array.size() - 1);

    std::cout << array[dis(gen)];
}

Notes:

rd:      Random device. Gives some initial randomness to help initialize things.
         Think of this as the `srand(time())` in the old random (but better).

mt19937: This is the algorithm used to generate the random number.
         It is initialized with some random value from rd. But it
         is a well know well understood random algorithm.

         Also be seperating this out into its own object.
         We don't have a central random number place. This means
         different applications can have their own unique random number stream.

         Note: If you want a random number stream. But the same stream
         every time (testing/debugging) then don't use the random
         device to initialize this object. Just use a nice normal
         integer and it will give a random stream of numbers but each
         time you run the application you get the same stream (which is useful
         for debugging when you don't actually want real randomness).


dis:     The main issue with the old rand() is that if just gave a number.
         It was up to the user of the number to build appropriate distributions
         and to be blunt most people either got it wrong or did not bother.
         In the random library there are several built in distributions but uniform
         is a nice easy one that is often useful (and I hope obvious).
Martin York
  • 257,169
  • 86
  • 333
  • 562
0

rand() doesn't return a true-random, rather it returns a pseudo-random. It all depends on the initial seed you provide to the random generator. If the initial seed is the same, then the consequent numbers you'll get from pseudo-random-algorithm is the same.

Then you should change the initial seed for the rand() on each call (in this case, each execution of your program). What else is a better changing value than time?

Note:

array[rand() % array[j]]; line on your code is highly vulnerable to segmentation fault by array index going out of bound.

Here is the solution.

#include <iostream>
#include <time.h>

using namespace std;

int main()
{   
    // Initialize the srand seed.
    srand (time(NULL));

    int size = 5;
    int array[size];
    int Random;
    for (int i = 0; i <5; i++)
    {
        cin>>array[i];
    }

    int index = rand() % size;
    Random = array[index];
    cout << Random << endl;
}

UPDATE:

As many others suggested, you can move to std::uniform_int_distribution for better results. My answer only updates your initial code.

Praneeth Peiris
  • 2,008
  • 20
  • 40
  • 1
    Addendum: Consider using [`std::uniform_int_distribution`](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) from the [`` library](https://en.cppreference.com/w/cpp/numeric/random) rather than `srand`/`rand`. If you do not want repetition, look into the example at the bottom of [`std::shuffle`'s documentation page](https://en.cppreference.com/w/cpp/algorithm/random_shuffle) – user4581301 Sep 21 '18 at 14:53
  • `int size = 5; int array[size];` is not valid C++. Some compilers provide it as an extension. Make `size` a `const`; there's no need for it to be modifiable here, even if it was legal. – Pete Becker Sep 21 '18 at 15:24
  • If you are going to use `rand()` than please please for the love of god make sure the range is good. Throw away any values of rand() that are greater than `RAND_MAX / size * size` otherwise your probability distribution is not even across all members. – Martin York Sep 21 '18 at 16:10