This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

rand function

I would like to control the output of the below random function. I want that it return a number between the min and the max parameters. but it didn't. can you tell what should I change to do that.
in the web, I found that 10000 should be the maximun that can be generated

I use a f340 with the tcp-ip conf wiz.

int random(int min, int max){ int res = min + (int)(my_rand()/10000*(max-1)); return res;
}

Parents
  • I took the code on the web.

    Don't blindly take code from the web without understanding what it does.

    res = ( my_rand()%(max-min) ) + min;
    


    did exactly what I want.

    Are you really sure it does exactly what you want ? The above line will not produce pseudorandom numbers that are evenly distributed (i.e. each of the numbers has the same probability of occurring). It's like rolling loaded dice.

Reply
  • I took the code on the web.

    Don't blindly take code from the web without understanding what it does.

    res = ( my_rand()%(max-min) ) + min;
    


    did exactly what I want.

    Are you really sure it does exactly what you want ? The above line will not produce pseudorandom numbers that are evenly distributed (i.e. each of the numbers has the same probability of occurring). It's like rolling loaded dice.

Children