Forex



Go Back   Forex Trading > Programming > MetaTrader
Forex Forum Register More recent Calendar Advertising Others Help






Register
Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.

From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.

Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
  • Elite Section
    Get access to private discussions, specialized support, indicators and trading systems reported every week.
  • Advanced Elite Section
    For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
See more

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-19-2009, 10:39 PM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 820
wolfe is on a distinguished road
Random Number Generation

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #2 (permalink)  
Old 02-19-2009, 11:35 PM
Senior Member
 
Join Date: Feb 2006
Posts: 587
Michel is on a distinguished road
Quote:
Originally Posted by wolfe View Post
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
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:
PHP Code:
double Rnd + (y-x)*MathRand()/32767
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #3 (permalink)  
Old 02-19-2009, 11:40 PM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 820
wolfe is on a distinguished road
Quote:
Originally Posted by Michel View Post
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:
PHP Code:
double Rnd + (y-x)*MathRand()/32767
Thank You Michel!

I will give this a try and let you know how it works.

Thanks again.

-wolfe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #4 (permalink)  
Old 02-19-2009, 11:56 PM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 820
wolfe is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #5 (permalink)  
Old 02-20-2009, 12:55 AM
darkkiller's Avatar
Senior Member
 
Join Date: Jul 2007
Location: Malaysia
Posts: 258
darkkiller is on a distinguished road
How about random text?What code for it?
Example I have 20 text,and i want it appear randomly?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #6 (permalink)  
Old 02-20-2009, 01:45 AM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 820
wolfe is on a distinguished road
Quote:
Originally Posted by darkkiller View Post
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:

PHP Code:
double Rnd 20 + (0-20)*MathRand()/32767;

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");} 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #7 (permalink)  
Old 02-20-2009, 07:31 AM
darkkiller's Avatar
Senior Member
 
Join Date: Jul 2007
Location: Malaysia
Posts: 258
darkkiller is on a distinguished road
I just appear the Number from 1 to 20,the text still not appear?
Why it happen?

Code:
//+------------------------------------------------------------------+
//|                                                 Random Qoute.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  double Rnd = 20 + (0-20)*MathRand()/32767;

if (Rnd == 1) {Comment(" i love u mama");}
if (Rnd == 2) {Comment(" i love u papa");}
if (Rnd == 3) {Comment(" i love u darling");}
if (Rnd == 4) {Comment(" i love myself");}
if (Rnd == 5) {Comment(" i love my country");}
if (Rnd == 6) {Comment(" i love forex");}
if (Rnd == 7) {Comment(" i love my son");}
if (Rnd == 8) {Comment(" i love my car");}
if (Rnd == 9) {Comment(" i love my cat");}
if (Rnd == 10) {Comment(" i love my wife");}
if (Rnd == 11) {Comment(" i love my mobilephone");}
if (Rnd == 12) {Comment(" i love my money");}
if (Rnd == 13) {Comment(" i love GJ");}
if (Rnd == 14) {Comment(" ilove dadadada");}
if (Rnd == 15) {Comment(" i love margincall");}
if (Rnd == 16) {Comment(" i love my computer");}
if (Rnd == 17) {Comment(" i love u");}
if (Rnd == 18) {Comment(" i love my bed");}
if (Rnd == 19) {Comment(" i love win");}
if (Rnd == 20) {Comment(" hello there");}  
{ Comment(Rnd); return(0);}
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
Attached Files
File Type: mq4 Random Qoute.mq4 (2.3 KB, 6 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #8 (permalink)  
Old 02-20-2009, 10:19 AM
Senior Member
 
Join Date: Feb 2006
Posts: 587
Michel is on a distinguished road
Quote:
Originally Posted by darkkiller View Post
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:
PHP Code:
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[Index]); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #9 (permalink)  
Old 02-20-2009, 02:16 PM
Senior Member
 
Join Date: Jan 2007
Location: not so remote
Posts: 3,272
fxbs is on a distinguished road
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);
Attached Files
File Type: mq4 Laguerre_RSI_v1.01_mtf.mq4 (10.9 KB, 4 views)

Last edited by fxbs; 02-20-2009 at 02:22 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #10 (permalink)  
Old 02-20-2009, 02:44 PM
kol8701's Avatar
Member
 
Join Date: Sep 2008
Posts: 79
kol8701 is on a distinguished road
need...

Can someone help me post Round Number Indicator that would plot itself on round numbers on my chart??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random entry EA Beno General Discussion 42 12-16-2008 06:34 AM
Random Generator Ronald Raygun MetaTrader 3 03-11-2008 05:34 AM
Random Walk indicator raff1410 Indicators - Metatrader 4 21 08-23-2007 10:57 AM
Intrabar Order Generation? alpha24seven Expert Advisors - Metatrader 4 2 05-04-2007 01:18 AM


All times are GMT. The time now is 12:25 AM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.