Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ThreadSafe Random Number Generator?

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

    ThreadSafe Random Number Generator?

    Is there a built in ThreadSafe Random Number generator that can be called through NinjaScript? This would be handy for OCO Codes as well as other Machine Learning applications.

    #2
    You might try using GetAtmStrategyUniqueID(). This method simply returns Guid.NewGuid().ToString("N"), providing you with a .NET Guid structure containing a unique integer ID. Other than this, we do not have a method solely designed to provide a random number. However, because of the vast number of possible combinations generated by a Guid, it is pretty safe to assume its uniqueness.

    Based on what I'm seeing online, it seems that Guid properties can be made thread-safe:

    http://stackoverflow.com/questions/1...ies-threadsafe

    http://stackoverflow.com/questions/8...-guid-property
    Last edited by NinjaTrader_DaveI; 08-26-2015, 07:30 AM.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      There is a C# method that produces random numbers:

      Get random numbers with the Random class. Random has a Next method and can be used as a field.


      I don't know how this would be 'imported' into NT7.

      Otherwise, you could use something following the computer pioneer (and world-class math genius!) John von Neumann. You could find square roots from any number of NT indicator doubles. This is from Wikipedia:



      An early computer-based PRNG, suggested by John von Neumann in 1946, is known as the middle-square method. The algorithm is as follows: take any number, square it, remove the middle digits of the resulting number as the "random number", then use that number as the seed for the next iteration. For example, squaring the number "1111" yields "1234321", which can be written as "01234321", an 8-digit number being the square of a 4-digit number. This gives "2343" as the "random" number. Repeating this procedure gives "4896" as the next result, and so on. Von Neumann used 10 digit numbers, but the process was the same.

      A problem with the "middle square" method is that all sequences eventually repeat themselves, some very quickly, such as "0000". Von Neumann was aware of this, but he found the approach sufficient for his purposes, and was worried that mathematical "fixes" would simply hide errors rather than remove them.

      Von Neumann judged hardware random number generators unsuitable, for, if they did not record the output generated, they could not later be tested for errors. If they did record their output, they would exhaust the limited computer memories then available, and so the computer's ability to read and write numbers. If the numbers were written to cards, they would take very much longer to write and read. On the ENIAC computer he was using, the "middle square" method generated numbers at a rate some hundred times faster than reading numbers in from punched cards.

      The middle-square method has since been supplanted by more elaborate generators.

      Comment


        #4
        Originally posted by arbuthnot View Post
        And for more information on thread safety with the Random class, take a look at this MSDN reference:

        https://msdn.microsoft.com/en-us/lib...x#ThreadSafety
        Dave I.NinjaTrader Product Management

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by algospoke, Today, 06:40 PM
        0 responses
        9 views
        0 likes
        Last Post algospoke  
        Started by maybeimnotrader, Today, 05:46 PM
        0 responses
        7 views
        0 likes
        Last Post maybeimnotrader  
        Started by quantismo, Today, 05:13 PM
        0 responses
        7 views
        0 likes
        Last Post quantismo  
        Started by AttiM, 02-14-2024, 05:20 PM
        8 responses
        168 views
        0 likes
        Last Post jeronymite  
        Started by cre8able, Today, 04:22 PM
        0 responses
        9 views
        0 likes
        Last Post cre8able  
        Working...
        X