Announcement

Collapse
No announcement yet.

Random function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Random function

    Hi All,

    Is there a random function in EFS (Don't ask ;-)?

    Garth
    Garth

  • #2
    Garth,

    You can use the Math.random() method, or alternatively, use the random function in the attached efs.

    I have included both methods in the attached efs. It is a stand alone efs that can be run. I output to the formula output window and create a file in the C:\Program Files\eSignal\FormulaOutput directory named random variable methods Trace.txt

    I hope this helps

    Regards,

    PHP Code:
    var barcount 0;var ntime;

    // The Central Randomizer 1.3 (C) 1997 by Paul Houle ([email protected])
    // See:  [url]http://www.msc.cornell.edu/~houle/javascript/randomizer.html[/url]
    rnd.today=new Date();
    rnd.seed=rnd.today.getTime();
    function 
    rnd() {
            
    rnd.seed = (rnd.seed*9301+49297) % 233280;
            return 
    rnd.seed/(233280.0);
    };
    function 
    rand(number) {
            return 
    Math.ceil(rnd()*number);
    };
    // end central randomizer. -->

    function preMain(){
    }
    function 
    main(){
    //put stuff u want implemented every tick in here
    //
        
    if (getBarState() == BARSTATE_ALLBARS) {//true once during initial load and subsequent reload
            
    barcount 0;
        }
        if (
    getCurrentBarIndex() < -50 )return;
        
        if (
    getBarState() == BARSTATE_NEWBAR) {     
            
    barcount ++;
            
    trace(" ");
            var 
    rand(105);
            
    trace("barcount = "+barcount+" random number = "+a);
            
    trace("Math.random() method = "+Math.random());
        }
        if (
    getCurrentBarIndex() == -){
        }
    //put stuff u want implemented every tick in here
    }

    //########### trace section required to be copied complete ##############
    var tracedata = new Array();
    //var traceon = true;// previous tracefile is overwritten if this is true
    var traceon false;// previous tracefile is added to if this is false
    var    tracefile = new File("random variable methods Trace.txt");
    function 
    trace(data1){//routine that saves the data
            
    debugPrintln(data1);
            if (
    traceon){//first use is true, overwrites previous trace file
                
    traceon false;
                
    tracefile.open("wt+");tracefile.close();//opens and overwrites previous file
            
    }
            
    tracefile.open("at+");
            
    tracefile.writeln(data1);
            
    tracefile.close();
    }
    //}
    //sample format to save data to file
    //trace("69:btest = "+btest);

    //########## section required to be copied complete ################ 
    Attached Files

    Comment


    • #3
      Thanks Steve. A question, do you have any insight as to "how random" either of these generators are? (ie: in the past I have used random number generators that produced not-so-random random numbers).

      Thanks again!

      Garth
      Garth

      Comment


      • #4
        Garth,

        No, I do not. If you are curious, you could put the calls to the random number generator in a do loop, run it 10,000 times, then plot them in excel or similar package.

        If that does not turn out right, you may want to try and use one to feed into the other and see if that gets you anywhere.

        One last option would be to check out the website I grabbed the one random number function http://developer.irt.org/script/343.htm

        Let me know if this works out for you.

        Regards.

        Comment


        • #5
          Thanks again Steve,

          Think I'll just plot it in eSignal in a study pane ;-P

          I'll let you know what I find.

          Garth
          Garth

          Comment


          • #6
            Hi Steve,

            OK, reporting back FYI...

            Attached should be a chart showing the results of Central Randomizer v1.3 vs. Math.random().

            Test:

            For both Central Randomizer and Math.random() run a test that
            for each bar on a 2753 bar chart, generate 4 random numbers from 1-100 (11012 total numbers generated). Track each time a number was picked.

            What you will see:

            Each bar represents a number from 1-100. The length of the bar shows how many times that number was generated by that study (Math.random() on top, Central Randomizer on bottom).
            The red line represents the average of all numbers generated.

            Results:

            With this number of samples, there should be fairly even distribution, so the lines should be about equal in height. Both do an OK job it seems, but Math.random seems to generate a bit more even distribution, and therefore may be better (on three run's of this, it was always true that Math.random() seemed better).

            Both generated an average of about 50 (I rounded to nearest whole number), which is good. Something would have been badly out of whack here if it didn't.

            Math.random was **MUCH** faster. Even with the trace statements commented out of the CR, Math.random() was much faster. I suspect the time function in CR is slowing things down a lot.

            Hope this helps someone...

            Garth

            Late Addition: The second red line in the image on top is the cursor...ignore it.
            Attached Files
            Last edited by gspiker; 06-19-2004, 10:25 PM.
            Garth

            Comment


            • #7
              Garth,

              Nice test methodology for evaluating the two different methods. Based on the work you have done, it seems the built in is clearly the one to use. I very much appreciate the effort!

              Regards,

              Comment

              Working...
              X