Hi,
I thought that MathRand() returns an Integer value within the range of 0 to32767 based on MathSrand().
Can somebody please tell me why this script always prints the same number?
How can I modify it that I receive different numbers?
Read the documentation
It is because you are seeding with the same value (Bars) every time.
The documentation recommends using
MathSrand(GetTickCount())
Here is why I need it. I made a small script which plots a horizontal trendline I like to use for my weekend analysis. I use it by a hotkey and then I just move the line where I want to place it. Much faster and easier for me instead of drawing it by the mouse. So far, so good. Plotting the lines when the markets are open works fine because a part of the objects' name is TimeCurrent(). But when I plot them on the weekend when markets are closed there is a problem because TimeCurrent() doesn't change of course. Therefore I wanted a random number to be calculated that I can use my script also at the weekends.
Here is the script:
void OnStart() { //--- double YMid = (WindowPriceMax() + WindowPriceMin())/2; int XMid = WindowBarsPerChart()/4; datetime begin = iTime(NULL, 0, XMid); MathSrand(Bars); int rndNo = MathRand(); ObjectCreate("Level "+(string)TimeCurrent()+"_"+(string)rndNo, OBJ_TREND, 0, begin, YMid, Time[0]+PeriodSeconds(), YMid); ObjectSet("Level "+(string)TimeCurrent()+"_"+(string)rndNo, OBJPROP_RAY_RIGHT, false); ObjectSet("Level "+(string)TimeCurrent()+"_"+(string)rndNo, OBJPROP_COLOR, clrWhite); ObjectSet("Level "+(string)TimeCurrent()+"_"+(string)rndNo, OBJPROP_SELECTED, true); }
Can anyone tell me how I can fix that? I have no idea how to give the lines different names when nothing (time, price) is running.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I thought that MathRand() returns an Integer value within the range of 0 to 32767 based on MathSrand().
Can somebody please tell me why this script always prints the same number?
How can I modify it that I receive different numbers?