Jump to content

Recommended Posts

Posted

I am trying to generate a random number between i and j.

 

The code at the bottom always generates the same number which is 42.

 

How can I fix this ?

 

 

int RandInt (int i , int j)

{

return rand( ) % (j-i+1) + i ;

}

Posted

Is this C?

 

Have you seeded the random number generator before using it? If you don't, it'll be seeded with the same value each time you run the program, and it will generate the same sequence of random numbers every time.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.