realbasic-games
[Top] [All Lists]

Re: Random Class

To: REALbasic Games <realbasic-games at lists dot realsoftware dot com>
Subject: Re: Random Class
From: Phil M <phil at mobleybros dot com>
Date: Tue, 25 Oct 2005 07:46:22 -0700
Delivered-to: realbasic-games at lists dot realsoftware dot com
References: <435E2583 dot 3090406 at earthlink dot net>
On Oct 25, 2005, at 5:30 AM, Eric Richards wrote:

I have a function that returns a random number and I'm
updating it to use the new random class.

In the LR is says that you should only create one random object. So should I make, say Rd, a property in the window instead of using it in the function?

My app will be using this function more than once.

You can have more than one Random instance especially if you want to take advantage of predictive random algorithms. The reason the docs state to only have one copy is that the random behavior is less effective when you create multiple instances -- especially if you create a new instance every time a method is called... take this for example:

  Function RndInRange(low As Integer, high As Integer) As Integer
    Dim r As New Random
    Return r.InRange(low, high)
  End Function

Calling this function multiple times rapidly will show sets of the same number (since the time seed is unchanged). You can get around this limitation if you use the Static keyword (RB 2005 and higher), since the code on the Static line gets executed only on the first call to the method:

  Function RndInRange(low As Integer, high As Integer) As Integer
    Static r As New Random
    Return r.InRange(low, high)
  End Function

Another approach is to have a Random class property as part of a Module or in the App class, so that Random functions could be called within any place in the application.
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>