I'm getting then same rnd-nmbr;s every time "in C"...why ?

Hello.

Can "you" tell me why the same numbers occur every time.

I whant different , each time.


Thanks in advance.


#include

.

.

.

random();

for ( t = 0; t < 10; t++){

rnd_s[t] = rand() % 10;

}







Comments

  • : Hello.

    : Can "you" tell me why the same numbers occur every time.

    : I whant different , each time.


    : Thanks in advance.


    : #include

    : .

    : .

    : .

    : random();

    : for ( t = 0; t < 10; t++){

    : rnd_s[t] = rand() % 10;

    : }


    You have to initialize the random number generator. Use the function randomize.






  • Hello. DAIMON.

    I've initilized , but still gets

    the same numbers every time.

    I have a djgpp compiler.

    I've tried but i dont know what I'm doing wrong.

    Do you know..??


    Greatings

    John.




  • : Hello. DAIMON.

    : I've initilized , but still gets

    : the same numbers every time.

    : I have a djgpp compiler.

    : I've tried but i dont know what I'm doing wrong.

    : Do you know..??


    : Greatings

    : John.


    If you want to have different numbers every time you run the program, you have to seed the random number generator different every time. The easiest way to do this is to seed with current time.


    unsigned int seed;


    // ...... put current time in seed here.


    srand(seed);


  • The following code works on most compliers and can be used to form the basis of your code.


    // seed the sequence first ...

    time_t now = time(NULL);

    srand((unsigned long)now);


    // use rand() to get some numbers ...


    Please note - you will have to re-seed the sequence if you start a new thread and want to

    use rand() in the new thread.


    Hope this helps


    Derek




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories