We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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; }
"the below random function"
Be sure to understand that it is a pseudo-random function!
"but it didn't."
So what did it do? What thought have you given to why that might be, and how it could be changed to your requirement?
You haven't shown your definition of 'my_rand' - so how can anyone tell what it might be doing?!
Please also read the instructions on how to post source code: www.danlhenry.com/.../keil_code.png and be sure to pay attention to the preview!
What value does my_rand() return ?
Excuse me I have forgotten to add it int my_rand () { static int first = 0; if (first == 0) { srand (43); first = 1; } return rand (); }
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.
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.
You have also forgotten (a second time) to read the information how to post source code, despite Andy specifically notifying you about it - and posting a link.
Now, it is time for you to pick up the documentation for rand(). It normally returns integer values between 0 and RAND_MAX, so RAND_MAX should be a good constant to make use of.
It is quite unlikely that your RAND_MAX is 10000. A more common value is 32767 or 65535 or any other 2^n-1 value.
sorry for the wrong code and thanks for your answer. I took the code on the web. res = ( my_rand()%(max-min) ) + min; did exactly what I want. thanks
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.
I use this function to simulate a graphic chart, I just want that values change each time. I understand what you said about probability of occuring. can you tell me the way to make it right?
I understand what you said about probability of occuring. can you tell me the way to make it right?
Somewhat better in terms of distribution would be:
long tmp; tmp = my_rand(); tmp = tmp * (max - min); tmp = (tmp + (MAX_RAND / 2)) / MAX_RAND; tmp = tmp + min;
Of course, the price to pay is additional computational effort (multiplication and division take a while on a '51, especially with long integers).
Of course, the price to pay is additional computational effort (multiplication and division take a while on a '51, especially with long integers). he clearly does not care about that, he uses an int for TRUE/FALSE. He crossposted at the SILabs forum www.cygnal.org/.../002446.html
Erik