3
votes

I am having trouble using srand.

i am trying to generate a random number in the interval 100 to 200.

The number will keep being generated and placed in an array. Once the method is called again the same sequence of random numbers needs to be generated again.

Because of this I need a seed, no matter what I try I cannot seem to get it to work.

I am not looking for anyone to write some code rather just show me the correct formatting for generating such numbers.

UPDATE

I have a train object, which contains a linkedlist(each position in the linedlist is a carriage).

The number of carriages in each train needs to be random in the interval 100, 200.

The amount of coal in each carriage needs to be random in the interval 1000, 2000.

I am trying to implement a simulator class that will create a train with a random amount of carriages which contain a random amount of data.

Hope that makes a bit more sense.

Struggling on how to implement it.

3
If you need same sequence again, what is the need to call the function again. Just store it in a container during its first call and reuse them.Mahesh
Post the relevant parts of code you have, someone should be able to fix whatever isn't working.Mat

3 Answers

2
votes

If you just want to repeat an arbitrary sequence, you can set the seed with srand() by giving it the same argument.

For example:

pax$ cat qq.c
#include <iostream>
#include <cstdlib>

int main (void) {
    srand (42);
    for (int i = 0; i < 5; i++) {
        int x = 100 + (rand() % 101);
        std::cout << x << std::endl;
    }
    std::cout << "=====" << std::endl;
    srand (42);
    for (int i = 0; i < 5; i++) {
        int x = 100 + (rand() % 101);
        std::cout << x << std::endl;
    }
    return 0;
}

pax$ g++ -o qq qq.cpp ; ./qq
163
166
148
137
149
=====
163
166
148
137
149
1
votes

try this

void srand ( unsigned int seed );

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.

1
votes

As mentioned, you can srand by seeding it more than once with the same seed.

srand(1234); // magic number

// .... 

srand(1234); // magic number again

The output of rand() will be restarted from the same point as before.

While you're at it, I recommend against using the modulo operator on rand(), because it will not result in a uniform distribution of value. Instead, you can use the following helper to get a random value in an integer range:

int randRange(int M, int N)
{
     // see http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
     return M + rand() / ( RAND_MAX / ( N - M ) + 1 );
}

int nextrand = randRange(100,200);

See the linked article on Eternally Confuzzled (by Julienne Walker) for more background, also on seeding.

C++ options

The above has the drawback that the generation is entirely sequential (you can not have two instances of the random generator at the same time). Since you are tagging with C++, why don't we use it!

You can use tr1 or c++0x/c++111uniform_int_distribution:

#include <random>
#include <functional>

std::uniform_int_distribution<int> distribution(100, 200);
std::mt19937 engine; // Mersenne twister MT19937

int nextrand  = distribution(engine);

An immediate advantage is that you can have multiple engines simultaneously generating the same sequence (see sample).

As you can also see, you can seed the generator like engine(1234) just like with srand. See the sample live on:

  • C++03 with boost: https://ideone.com/FC4xm

    #include <boost/random.hpp>
    #include <boost/random/uniform_int.hpp>
    
    int main()
    {
        boost::mt19937 engine1(1234);
        boost::mt19937 engine2(1234);
        boost::uniform_int<> dist(100,200);
    
        for (int i=0; i<20; i++)
        {
             std::cout << dist(engine1) << " is equal to " << dist(engine2) << std::endl;
        }
    }
    

  • C++0x http://ideone.com/467Aj
    Also demonstrating a little syntactic sugar:

    auto generator = std::bind(distribution, engine);
    nextrand = generator(); // more convenient use
    

1 Boost.Random if you use C++03