Random Number Generation - page 2

 
wolfe:
It's because you had this at the end of your code:
{ Comment(Rnd); return(0);}[/PHP]

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.

thanks wolfe..now i see the trick in this code.thanks again

 
Michel:
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:

string Text[21] = {"I love papa", "I love mama", "I love my girlfriend", ...};

double Rnd;

int Index; // 0 to 20 : 21 values

Rnd = 20.999999*MathRand()/32767.0;

Index = MathFloor(Rnd);

Print(Text);

thanks also this Michel!!

 
Michel:
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:
double Rnd = x + (y-x)*MathRand()/32767;

May I also suggest Rnd = x + MathRand()%(y+1) This should be an int between x and y inclusive without extra code needed.

 

If you need truly random numbers, you can use "wininet.dll" to pull down one of those websites that generates numbers from atmospheric interference. Save one (or many) of these to a file, and reference it when needed.

It does halt processing though, so it's something to do in init() or not at all, depending how often it would have to be called.

 

Just found this small thread https://www.mql5.com/en/forum/178863 about MathSrand:

MathSrand(TimeLocal());

// Display 10 numbers.

for(int i=0;i<10;i++ )

Print("random value ", MathRand());

From: MathRand - MQL4 Documentation

I mean:

IN10TION:
:: yes it's possible... check this http://docs.mql4.com/math/MathRand

:: with the retrieved random number check into an array with string variables... the output will be a random string/text

IN10TION

 

:):):)


Seeding can be double (unlike the built in MathRand() function which has one seed) and all in all, given the simplicity of code and compared to some other ones, it is not a bad pseudo random generator at all. Attached is a simple indicator that uses that function (it generates something like the example bellow) and also attached a Visual C# source by John D. Cook which implements much more of a pseudo random number generation (worth at least taking a look at the code by all means)

Files:
random.gif  31 kb
simplerng.zip  3 kb
_rand.mq4  2 kb
 

would it be nice

if a random generator give us a pair to trade [[ random suggestion ]], when some momentum surge alarm sound

e.g. trade now -- for NZDJPY -or- consider to trade EUR/USD now

youtube code [ watch?v=k1JbiLl3vLw -- NOT HD TV recorded file ] they only sing HAPPY SONGS

enclose a scanner, but I don't think it is much use except you are look to look for V-turn apex -- have fun with it -- but disposable too , I think

Files:
Reason: