Hey, I have a generator but it works dependently on time, so it makes new values only every second. Is there a way to make it work faster? I want to be able to generate new numbers every millisecond if possible.

srand((unsigned)time(0));
    int a;
    int b;
    int c;
    int d;
    int e;
    int f;
    int g;
    int h;
    int i;
    int j;

    int lowest=0, highest=9;
    int range=(highest-lowest)+1;
        a = lowest+int(range*rand()/(RAND_MAX + 1.0));
        b = lowest+int(range*rand()/(RAND_MAX + 1.0));
        c = lowest+int(range*rand()/(RAND_MAX + 1.0));
        d = lowest+int(range*rand()/(RAND_MAX + 1.0));
        e = lowest+int(range*rand()/(RAND_MAX + 1.0));
        f = lowest+int(range*rand()/(RAND_MAX + 1.0));
        g = lowest+int(range*rand()/(RAND_MAX + 1.0));
        h = lowest+int(range*rand()/(RAND_MAX + 1.0));
        i = lowest+int(range*rand()/(RAND_MAX + 1.0));
        j = lowest+int(range*rand()/(RAND_MAX + 1.0));

cout << a<<b<<c<<d<<e<<f<<g<<h<<i<<j<<endl;

Recommended Answers

All 4 Replies

you only need to call srand(time(0)) once during the application's execution. So the function you have is not dependent on time and you can generate your random numbers as fast as you like (just take the srand() call out of your function and call it once at the start of the application only).

Is this code called in some kind of loop?
For example, if this was called as below:

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

using namespace std;


int main(void)
{

for( unsigned int i = 0; i < 100; i++ )
{
srand((unsigned)time(0));
    int a;
    int b;
    int c;
    int d;
    int e;
    int f;
    int g;
    int h;
    int i;
    int j;

    int lowest=0, highest=9;
    int range=(highest-lowest)+1;
        a = lowest+int(range*rand()/(RAND_MAX + 1.0));
        b = lowest+int(range*rand()/(RAND_MAX + 1.0));
        c = lowest+int(range*rand()/(RAND_MAX + 1.0));
        d = lowest+int(range*rand()/(RAND_MAX + 1.0));
        e = lowest+int(range*rand()/(RAND_MAX + 1.0));
        f = lowest+int(range*rand()/(RAND_MAX + 1.0));
        g = lowest+int(range*rand()/(RAND_MAX + 1.0));
        h = lowest+int(range*rand()/(RAND_MAX + 1.0));
        i = lowest+int(range*rand()/(RAND_MAX + 1.0));
        j = lowest+int(range*rand()/(RAND_MAX + 1.0));

cout << a<<b<<c<<d<<e<<f<<g<<h<<i<<j<<endl;
}

    return 0;
}

Then it should run as fast as it is processed. Is this code in a function that you call elsewhere because the code you have posted is a bit out of context and as is wouldn't even compile. Maybe posting a bit more of your code would make it easier to examine :)
Also, calling srand so many times with the same time seed is unnecessary and only needs doing maybe once at the start of your programs execution.
Oh and just out of curiosity, why are you even computing the value for range when the values used to do so are hard coded on the previous line?

never mind, I got it to work. Yes, it is part of a loop. My mistake was that I placed the seed inside the loop, so it kept on reseeding. (I didn't include the while loop in the code above).

Glad you've sorted it :)

P.s. Could you mark this thread as solved to get it out of the way and done with

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.