-3

i am trying to get random numbers using rand % (5 -5) and it gives division by zero error

#include <iostream>
using namespace std;
int main()
{
int A[20];
for (int i-=0 ;i<20;i++)
 A[i]= rand() % -5;

system("pause")
return 0;
}
sticky bit
  • 36,626
  • 12
  • 31
  • 42

5 Answers5

2

Just use std::uniform_int_distribution

Produces random integer values i, uniformly distributed on the closed interval [a, b]...

std::random_device rd;  //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> dis(-5, 5);
std::cout << dis(gen) << std::endl;

live example

Slava
  • 43,454
  • 1
  • 47
  • 90
0

rand returns :

a pseudo-random integral value between ​0​ and RAND_MAX (0 and RAND_MAX included).

To get a value from the range [-5, .., 5] (inclusive) you have to calculate it from the range [0, .., RAND_MAX].

In the range [-5, .., 5] there are 11 values [-5, .., -1, 0, 1, .., 5].

Starting from (and including) 0, counting 11 values, the final number is 10.

To return values [0, .., 10] from the bigger range, mod can be used. But you need in the results a 10 so a mod 11 has to be used.

Now to get a value to a range [-5, .., 5] from a range [0, .., 10], you need to subtract 5.

const int val_from_rand = rand();
const int range_0_10 = val_from_rand % 11;
const int random_val = range_0_10 - 5;
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
0

Use the new random library

#include <random>

// Seed with a real random value, if available
std::random_device r;
std::default_random_engine e1(r()); 
// A random number between - 5 and 5
std::uniform_int_distribution<int> uniform_dist(-5, 5);

int nextRandom()
{
    return uniform_dist(e1);
}

Which is called:

int r = nextRandom();

You can also create a generalized version:

template< int low, int high >
int nextRandom()
{
    static_assert( low < high, "low must be < high" );

    // Seed with a real random value, if available
    static std::random_device         r;
    static std::default_random_engine e1( r() );
    // A random number between low and high
    static std::uniform_int_distribution< int > uniform_dist( low, high );

    return uniform_dist( e1 );
}

Which is called:

int random = nextRandom< -5, 5 >();
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
0

You forgot ; after system("pause"). Apart from that, you were not far from the solution.

However reading the the reference you see that %5 would return numbers between 0 and 4, and %-5 does not make any sense.

You can return numbers between 0 and 10 (10 included) and then scale it down by subtracting -5:

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
    int a[20];
    for (int i=0; i<20; i++)
        a[i] = (rand() % 11)-5; //use %10 if you do not want 5 as apossible outcome
    system("pause");
    return 0;
}

That is the easiest way and closest to your solution.

Please do not use capital letters for variables unless they are constant.

roschach
  • 8,390
  • 14
  • 74
  • 124
-6

You can try this code

`

#include <stdlib.h>
#include <time.h>

int main()
{
   int number = 0;

   srand(time(NULL));

   do
   {
      number = rand();
   }while(number > 5);

return 0;
}

`