Results 1 to 4 of 4

Thread: Completely random?

  1. #1

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530

    Completely random?

    Ok now I need to generate a really large random number, between 9000000000000000000 and 9999999999999999999. Now the rnd function on its own doesn't generate a small enough number to be useful in the situation. I multiplied three rnd number together to get an extremely tiny number.

    The question:

    Have I introduced any bias?

    Here is the function:


    MsgBox Len(Random("9000000000000000000", "9999999999999999999"))

    Private Function Random(ByVal Min$, ByVal Max$) As Variant
    Dim x, y, z
    Randomize
    x = CDec(Min): y = CDec(Max): z = CDec(Rnd) * CDec(Rnd) * CDec(Rnd)
    Random = CDec(((y - x + 1) * z + x))
    If InStr(1, Random, ".") Then Random = Left$(Random, InStr(1, Random, ".") - 1)
    End Function

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I have no idea if you've introduced bias. I can suggest a way to get a long number without multiplying though - concatenate random numbers in some range like 0 - 9999 and use a string to store it.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    More subtle than you think.

    If your application is critical, I strongly advise some research in a local library or on the Web. Unless you are quite knowledgeable, you are likely to do something wrong.

    Random numbers are far more subtle than you might suspect. There are all sorts of traps and problems in their use and much controversy about the definition of them.

    In general, you must be aware that computer generated random numbers are referred to as pseudo random numbers by experts in this area of knowledge. This is to remind them that computers do not generate truly random numbers.

    Furthermore, the random numbers suitable for one purpose are likely to be lousy for another purpose.

    Instead of reinventing a wheel, do some research. A worthwhile pseudo random number generator almost always requires a fairly simple algorithm. I am sure that there is literature on this subject which would give you all the information you need to write your own generator.

    You must choose the generator to match the process you are simulating. Do you need Poisson, binomial, or normal statistics. Perhaps you need something else.

    To give you an idea of the possible pitfalls, are you aware that the VB Rnd function is a reasonably good, but not a precise generator for simulating craps? The random number generators built into previous versions of Basic (Quick Basic & earlier versions) were so bad at simulating craps that they indicated that the game favored the player.

    There is an On-line service which uses radioactive decay to provide subscribers with random numbers generated via some radioactive decay process. I believe that this process generates Poisson statistics, which is great if the process you are simulating requires Poisson statistics, but questionable otherwise. Perhaps there are other similar services.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  4. #4
    jim mcnamara
    Guest
    FWIW search in google for random number generator. The algorithm is dirt simple.

    Guv is right - most rnd's are not really random - they don't pass the Runs Test for randomness. See this site

    hth


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width