Features of the mql5 language, subtleties and tricks - page 135
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
You are very considerate, thank you. I made a mistake, I didn't put brackets, so I would have used the "unfair" function.
ZS: made corrections to the original post.
Please explain why this construction is better than a trivial rand()%max.
ZS: I see what you mean.
Well then - what if max>32767? Your function is of type uint, but generates the same maximum as rand(), i.e. 32767
You'd better use something like
You are very considerate, thank you. I made a mistake, didn't put brackets, so I would use "unfair" function.
ZS: made corrections to the original post.
Do you realize that if someone wants to use your honest function and write get_rand(10), it will work 3276 times slower than rand()%10
Well then - what if max>32767? After all, your function is of type uint, but generates maximum that rand(), i.e. 32767.
you'd better use something like
And MathRand() returns int, what if someone expects a number with six zeroes from it? Counting on some level of public. Here, you yourself know how to get > 32767.
They just often write rand()%3 without thinking twice and then see my message and realize that it will shift the probabilities.
Do you realize that if someone wants to use your honest function and write get_rand(10), it will work 3276 times slower than rand()%10
Well yes, very slow, for a cycle of a million calls a callback while will happen about 300 times. If your programs consist of bare for() where get_rand() is fidgeting, that would be critical, I guess.
And do you realise that the os scheduler is taking up a lot of CPU time and slowing down execution disproportionately to rand()? If you are in such a hurry, you need some kind of dos.
And MathRand() returns int, what if someone expects a number with six zeros from it? I'm counting on a certain level of public. Here, you yourself know how to get > 32767.
They just often write rand()%3 without thinking twice and then see my message and realize there will be a probability shift.
Well yes, very slow, for a cycle of a million calls a callback while will happen about 300 times. If your programs consist of bare for() where get_rand() is fidgeting, that would be critical, I guess.
And do you realise that the os scheduler is taking up a lot of CPU time and slowing down execution disproportionately to rand()? If you're in such a hurry, you need some kind of dos.
And MathRand() returns int, what if someone expects a number with six zeros from it? I'm counting on a certain level of public. Here, you yourself know how to get > 32767.
They just often write rand()%3 without thinking and see my message and realize that it will shift probabilities.
Well yes, very slow, for a cycle of a million calls a callback while will happen about 300 times. If your programs consist of bare for() where get_rand() is fidgeting, that would be critical, I guess.
And do you realise that the os scheduler is taking up a lot of CPU time and slowing down execution disproportionately to rand()? If you are in such a hurry, you need some kind of dos.
Don't be afraid to admit your mistakes, there's nothing scary or humiliating about it. We all make mistakes. It's normal.
It's much easier and more useful in terms of community perception to say, "Yes, I was wrong. Thank you." instead of trying to make excuses.
This works much faster and more universally:
You may speed it up if you recode it from ulong to uint, if you don't need big numbers (you need 3 rand() instead of five).
Don't be afraid to admit your mistakes - there's nothing scary and humiliating about it. We all make mistakes. It's okay.
It's easier and more helpful to the community to say, "Yeah, I was wrong. Thank you." Instead of trying to justify yourself.
After all, this option works much faster and more universally:
It can also be speeded up by recoding from ulong to uint, if you don't need big numbers (3 rand() instead of five).
So you are not confused by unfairness of your implementation (in terms of speed, by the way, it will be much slower)? Suit yourself.
Zy: dishonesty - generating different range numbers with different probabilities.So you are not confused by the unfairness of your implementation (it will be much slower in terms of speed, by the way)? Suit yourself.
Zy: dishonesty is generating different range numbers with different probability.Are you serious?
yes
https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator
yes
https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator
So you're willing to set fire to a $100 note to find a dime rolled under the bed?
You should not underestimate the role of probabilities. At multimillion iterations these "rolled pennies" will cost you quite tangible figures. Moreover, your code is obviously irrational at small ranges (constantly making five calls to rand() is exactly the "burning the note")
Six months ago this topic was already discussed on the forum, I suggested this option:
Forum on trading, automated trading systems and testing trading strategies
How to get a random number in N-depth range ?
Alexey Navoykov, 2018.12.31 01:25
My last code turned out to be incorrect. I made too much trouble with digits. Here is right variant and more concise at the same time: