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
  • in the web, I found that 10000 should be the maximun that can be generated

    The maximum output of rand() depends on RAND_MAX, which should be defined somewhere in the include files.

    Also, please read and follow the instructions on how to post code, in order to keep your code readable. They can be found above the text window.

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

    Ok - my_rand returns an integer value between 0 and RAND_MAX, which you then divide by 10000. This is an integer division. If RAND_MAX is 32767, then you'll only get four different values from this division: 0, 1, 2, and 3.

Reply
  • in the web, I found that 10000 should be the maximun that can be generated

    The maximum output of rand() depends on RAND_MAX, which should be defined somewhere in the include files.

    Also, please read and follow the instructions on how to post code, in order to keep your code readable. They can be found above the text window.

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

    Ok - my_rand returns an integer value between 0 and RAND_MAX, which you then divide by 10000. This is an integer division. If RAND_MAX is 32767, then you'll only get four different values from this division: 0, 1, 2, and 3.

Children
No data