S
S
Sdelan_v_CCCP2014-09-23 13:46:01
linux
Sdelan_v_CCCP, 2014-09-23 13:46:01

Why doesn't the value in the function change?

Hello! Here is the software.

void test_random(void)
{
    srand(time(NULL));
    u16 value = rand()%24;
    printf("value = %d\n", value);

}

void main(void)
{
     int i;
     for(i = 0; i < 10; i++) {
        test_random();
     }
    
}

here is the result
value = 12
value = 12
value = 12
value = 12
value = 12
value = 12
value = 12
value = 12
value = 12
value = 12
Why is he doing this? Why are the results the same and not different?
PS: Linux, gcc compiler

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2014-09-23
@Sdelan_v_CCCP

Because time doesn't have time to change in your ten cycles, and you use it to initialize the random number generator. Can take out srand for a cycle?

D
Don Kaban, 2014-09-24
@donkaban

Slightly away from the question. 21st century in the yard, 14th year ends.
Why not use the standard library?
www.cplusplus.com/reference/random

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question