Hello all,
I was wondering if anyone knew a good way to code a random number generator?
I understand I could use the MathRand() function, but this function will return a random number anywhere between 0 - 32767.
What I need to do is generate a random number between two parametars, x & y.
An example would be if x=50 and y=500. I need to code a function to return a random number between these two integers.
Does anyone know how to accomplish this?
Thanks,
wolfeI am not sure its a "good" random number generator (it might depend of the distribution you need). Nevertheless, here is how to change the boundaries:
I am not sure its a "good" random number generator (it might depend of the distribution you need). Nevertheless, here is how to change the boundaries:
Thank You Michel!
I will give this a try and let you know how it works.
Thanks again.
-wolfe
Michel,
This solution works great! Thank you so much for helping me solve this problem, I really appreciate your help.
This is such a great forum, thanks to contributers like yourself.
Best of luck with your trading.
-wolfe
How about random text?What code for it?
Example I have 20 text,and i want it appear randomly?
How about random text?What code for it? Example I have 20 text,and i want it appear randomly?
Could you assign each text string an integer associated with it?
such as:
if (Rnd == 1) {Comment(" Text comment #1");}
if (Rnd == 2) {Comment(" Text comment #2");}
if (Rnd == 3) {Comment(" Text comment #3");}
if (Rnd == 4) {Comment(" Text comment #4");}
if (Rnd == 5) {Comment(" Text comment #5");}
if (Rnd == 6) {Comment(" Text comment #6");}
if (Rnd == 7) {Comment(" Text comment #7");}
if (Rnd == 8) {Comment(" Text comment #8");}
if (Rnd == 9) {Comment(" Text comment #9");}
if (Rnd == 10) {Comment(" Text comment #10");}
if (Rnd == 11) {Comment(" Text comment #11");}
if (Rnd == 12) {Comment(" Text comment #12");}
if (Rnd == 13) {Comment(" Text comment #13");}
if (Rnd == 14) {Comment(" Text comment #14");}
if (Rnd == 15) {Comment(" Text comment #15");}
if (Rnd == 16) {Comment(" Text comment #16");}
if (Rnd == 17) {Comment(" Text comment #17");}
if (Rnd == 18) {Comment(" Text comment #18");}
if (Rnd == 19) {Comment(" Text comment #19");}
if (Rnd == 20) {Comment(" Text comment #20");}
I just appear the Number from 1 to 20,the text still not appear?
Why it happen?
To understand what we are doing :
MathRand() returns an integer between 0 and 32767, so
MathRand()/32767 is a double between 0 and 1
If we multiply this double by 20, we get a double between 0 and 20
If we need an integer, for example to index an array, we need to round it, but we cannot use MathRound() because the boundaries 0 and 20 will have only half of the probability to be hit than the other integer values.
So we can use MathFloor(), but then 20 will never be it, and thus we have to use 21 as upper boundary.
There is still one problem remaining: if MathRand() returns exactly 32767, then the result will be 21, and we don't want this. So instead of 21 we have to use a slighly smaller number like 20.999999.
The final code may be:
double Rnd;
int Index; // 0 to 20 : 21 values
Rnd = 20.999999*MathRand()/32767.0;
Index = MathFloor(Rnd);
Print(Text);
I love papa", "I love mama", "I love my girlfriend """"" - you mean - rendom?
---------------
also example : MathRand as in Mladen's UniqueName generator
string MakeUniqueName(string first, string rest)
{
string result = first+(MathRand()%1001)+rest;
while (WindowFind(result)> 0)
result = first+(MathRand()%1001)+rest;
return(result);
need...
Can someone help me post Round Number Indicator that would plot itself on round numbers on my chart??
I just appear the Number from 1 to 20,the text still not appear? Why it happen?
It's because you had this at the end of your code:
I coded this as an EA, and put a while loop in it with a 1000ms sleep() function, so your messages will change randomly every second.
I put this code in the init() function, since the market is currently closed and you will not get an incoming tick to fire the start() function.
[PHP]int init()
{
int num = 0;
while (num == 0)
{
double Rnd = 20 + (0-20)*MathRand()/32767;
if (Rnd == 1) {Comment("\n i love u mama");}
if (Rnd == 2) {Comment("\n i love u papa");}
if (Rnd == 3) {Comment("\n i love u darling");}
if (Rnd == 4) {Comment("\n i love myself");}
if (Rnd == 5) {Comment("\n i love my country");}
if (Rnd == 6) {Comment("\n i love forex");}
if (Rnd == 7) {Comment("\n i love my son");}
if (Rnd == 8) {Comment("\n i love my car");}
if (Rnd == 9) {Comment("\n i love my cat");}
if (Rnd == 10) {Comment("\n i love my wife");}
if (Rnd == 11) {Comment("\n i love my mobilephone");}
if (Rnd == 12) {Comment("\n i love my money");}
if (Rnd == 13) {Comment("\n i love GJ");}
if (Rnd == 14) {Comment("\n i love dadadada");}
if (Rnd == 15) {Comment("\n i love margincall");}
if (Rnd == 16) {Comment("\n i love my computer");}
if (Rnd == 17) {Comment("\n i love u");}
if (Rnd == 18) {Comment("\n i love my bed");}
if (Rnd == 19) {Comment("\n i love win");}
if (Rnd == 20) {Comment("\n hello there");}
//{ Comment(Rnd); return(0);}
Sleep(1000);
}
return(0);
}
Save in your experts folder and attach to a chart.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello all,
I was wondering if anyone knew a good way to code a random number generator?
I understand I could use the MathRand() function, but this function will return a random number anywhere between 0 - 32767.
What I need to do is generate a random number between two parametars, x & y.
An example would be if x=50 and y=500. I need to code a function to return a random number between these two integers.
Does anyone know how to accomplish this?
Thanks,
wolfe