Search code examples
crandomsrand

Random Number Generator with a Seed


I am generating random numbers by using srand(time(NULL)). Any idea why it always gives just even random numbers? In my case its giving so. Please help i need odd numbers too. I need the set of 0s, 1s. for eg : {1,1,0,0,1,0,0,0,1,1,0}


Solution

  • Think of initializing the PRNG like initializing a variable ... You don't do

    // pseudo-code
    // print numbers from 1 to 10
    do 10 times
        number_to_print = 1
        print number_to_print
        number_to_print++
    end loop
    

    Likewise, srand() should only be called once per program run.

    call srand() // initialize PRNG
    loop
        rand()
    end loop