-5

I'd like to generate numbers between -30 and 30.

I already tried the solution in Generating random integer from a range and it gave me these results:

111875657664
151875657664
211875657664
-41875657664
-151875657664
171875657664
-201875657664
-131875657664
-301875657664
-271875657664

This is my function:

int Random::genRandom() {
    int rnd;
    rnd = (rand()%61)-30;
    return rnd;
}

This is the main source file:

#include "Random.h"
#include <iostream>
using namespace std;

int main() {
    Random random;
    int randomas;
    for(int i=0; i<10; i++) {
        randomas = random.genRandom();
        cout << randomas << "\n";
    }
    return 0;
}

What should I do?

Community
  • 1
  • 1
user1885868
  • 1,063
  • 3
  • 17
  • 31

3 Answers3

2

Try this:

srand (time(NULL));
for(int i = 0;  i < 10; i++)
{
    int rnd;
    rnd = rand() % (60) - 30;

    cout << rnd << std::endl;
}

Working example here

nanomader
  • 410
  • 9
  • 22
2

This does nothing to solve the OP's bizarro number problem (which I can't seem to reproduce), but just to get this out there. C++11 and better provide a number of different ways to resolve the rand sucks issue.

I, for one, welcome our new random number generating overlords.

#include <iostream>
#include <random>

int main()
{
    std::random_device device;
    // build a random number generator for seeding
    std::default_random_engine engine(device()); 
    // nothing fancy. Assuming the compiler implementors know what they are doing
    // seeding it with a nice random number from above.
    std::uniform_int_distribution<int> distribution(-30, 30);
    // generate uniformly distributed numbers from -30 to 30

    int randomas;
    for(int i=0; i<10; i++) {
        randomas = distribution(engine); // Bless me with a number, divine masters!
        std::cout << randomas << "\n"; // Witness the number all shiny and chrome!
    }
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • You forgot to seed std::default_random_engine. You should probably use std::random_device for that purpose. – Fibbs Sep 08 '15 at 21:53
  • Yeah, just spotted that. Interesting the differences between cppreference and cplusplus. – user4581301 Sep 08 '15 at 21:54
  • Seems to work without it, but probably some nasty side effect I won't see until too damn late. Regardless, fixed. – user4581301 Sep 08 '15 at 21:55
  • It'll work without it, but you'll always have the same default seed (probably), so you'll always end up with the same set of random numbers. – Fibbs Sep 08 '15 at 21:58
1

Try this code. It is an application of a linear congruence generator to your problem. The generator has very good properties.

#include <iostream>

int main( int argc,char** argv )
{
//the seed, an arbitrary initial value
int seed = 338;

//these constants define a linear congruence generator
int modulus = 2147483399;
int multiplicator = 40692;
int m = 52774;
int l = 3791;

//additional variables
double max = static_cast<double>( modulus );
double value;

//number of random numbers
int N = 20;

//random number generation
int lcg_value = seed;

int k1;
int z;

for( int i = 0; i < N; ++i )
{
    k1 = lcg_value / m;
    z = multiplicator * ( lcg_value - k1 * m ) - k1 * l;

    if( z < 0 )
    {
        z = z + modulus;
    }

    lcg_value = z;

    //random number, lies between 0.0 and 1.0
    value = static_cast<double>( lcg_value ) / max;

    //print random number, lies between -30.0 and 30.0
    std::cout << ( 60.0 * value - 30.0 ) << std::endl;
}

return( 0 );
}
sperber
  • 661
  • 6
  • 20
  • 3
    You really think the problem is that he's using the built-in `rand()` and that writing his own RNG is the solution? – Barmar Sep 08 '15 at 20:29
  • 3
    This answer doesn't explain the erroneous results the op is getting. I'm also dubious about the merits of suggesting the op implement his own linear congruence generator when std::linear_congruential_engine exists. – Fibbs Sep 08 '15 at 20:33
  • Random number generation is not just about writing bug free code. If you are serious about generating random numbers, it is mandatory to write your own implementation. Many bad things have happened cause people blindly used rand(). Therefore I find it is a viable option to point out that it is not hard to write a generator: you have full control, and in the case of this generator you can rely on scientific publications which attest that the generator has excellent mathematical properties. – sperber Sep 08 '15 at 20:45
  • 3
    "it is mandatory to write your own implementation" — Umm, no, there are pre-written _bug-free_ implementations in the standard `` header, no need to roll your own (possibly introducing some bugs in the process). Using `rand()` is bad but rolling your own generator is not much better. Yes you get a huge amount of control, but with a huge amount of control comes a huge amount of responsibility, and a huge amount of potential for bugs. 99.9% of the time you will get just fine along with existing implementations. – Emil Laine Sep 08 '15 at 20:59
  • 2
    rand() is free to use whatever implementation it likes which is why it causes problems. [std::linear_congruential_engine](http://en.cppreference.com/w/cpp/numeric/random/linear_congruential_engine) is guaranteed to use a specific algorithm and even comes with typedefs for recommended peer reviewed parameters. It really isn't necessary to duplicate the code if you have access to it. – Fibbs Sep 08 '15 at 21:03
  • @Fibbles: Ok, I did not know that a proper lcg was part of the standard by now. Though - after taking a first look - I personally would not use the generators promoted there ;). In any case, I see that this is a viable option. I still hold up the claim that if you are serious about random number generation, you must not use rand(). – sperber Sep 08 '15 at 21:22