Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: RANDOM isn't so random.

  1. #1

    RANDOM isn't so random.

    Hello.

    I've found that "RANDOM (n)" returns some numbers more often than others. the game I was coding needs to select a random number from [0..20] and I get [5..15] much more often than the others, and [5..10] more than [11..15]. It's too obvious for me and I need a more regular frequency. I'm using RANDOMIZE too. [edit]I must say that these numbers are selected few times. In a 'standard' play it searches about 10-15 numbers each player.[/edit]

    Is it usual? Is there a way to "normalize" it or another random algorithm available?

    I'm using Lazarus 0.9.22 (FPC 2.0.4). If FPC 2.2.0 fixes it, how can I update Lazarus' compiler?

    Thanks in advance.
    No signature provided yet.

  2. #2

    RANDOM isn't so random.

    pascal random gives you a number at range from a list generated at app init {not sure}

    to corrent use a code like the above
    [pascal]
    Randomize;
    RandSeed := GetTickCount; // gettickcount is from windows unit, if you don't use it declare the following line
    // function GetTickCount: Cardinal; stdcall; external 'kernel32.dll';
    random(n); //now random will be a true random
    [/pascal]

    There are better ways but this one works perfectly with 2 lines added
    From brazil (:

    Pascal pownz!

  3. #3

    RANDOM isn't so random.

    From brazil (:

    Pascal pownz!

  4. #4

    RANDOM isn't so random.

    Quite a few years ago I asked something similair. I'm not really sure if it's what you're after as well, but here's the thread

    [off topic]I just discovered that if you do a search for that particular thread, you wont find it. :? Makes you wonder what else stays hidden n pgd.[/offtopic]

  5. #5

    RANDOM isn't so random.

    I'm curious on how did you made the testing?
    Firstly there is:
    21 numbers in 0..20
    10 numbers in 0..4 + 16..20
    11 numbers in 5..15

    Would it differ if using floating point random instead of integer random() or different value range?

    Then to have basics clear, examples:
    Integer random: Random(10) returns integer [0..9]
    Floating random: Random() returns ]0..1[ (never returns exactly 0 or 1)

    Edit: Also Randomize; should only be called once when application starts. After that simply Random() calls.

  6. #6

    RANDOM isn't so random.

    Use RandomRange from Math unit
    [pascal]{Description
    The RandomRange function generates a random Integer number within the range RangeFrom to RangeTo inclusively. *

    This provides a more convenient version of the System unit Random function.

    Both use a pseudo random number sequence of 232 values. Each time you run your program, the values generated will be the same, unless you reposition the generator to a different part of the sequence using the Randomize or RandSeed functions.}
    function RandomRange ( const RangeFrom, RangeTo : Integer ) : Integer;[/pascal]
    From brazil (:

    Pascal pownz!

  7. #7
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    RANDOM isn't so random.

    15 numbers isn't a very big sample to use and then say it isn't random. Using true randomisation means that at one time you could (very small chance of) getting 15 values of 5s in your sample.

    I did a similar test useing 1000 random checks, I never get one number with more than 2599 or less than 2400 over 10 runs. Seems pretty normal for me. [1000 runs means 0-4 on average 2500 times]
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  8. #8

    RANDOM isn't so random.

    Free Pascal uses the Mersenne Twister for random numbers:

    http://en.wikipedia.org/wiki/Mersenne_twister

    As the Mersenne Twister has a mathematical underpinning, I would not expect it to fail such a simple distribution test; it is expected to provide uniform distribution over [0..20].

    Do the numbers you get are really contrary to what you would mathematically expect? For example, one rule of randomness is that when you draw 10 numbers, on average sqrt(10) numbers will appear twice in the drawn set.

    I don't recommend playing with randseed as it is "emulated"; the true random seed for the Mersenne Twister is an array of values inside the system unit.

  9. #9

    RANDOM isn't so random.

    In one of the Game Programming Gem books there is an article about creating a true randomizer, reading different inputs, system states, clocks, etc and combining them all. It's very interesting, if you want to take a look at it.

  10. #10

    RANDOM isn't so random.

    Quote Originally Posted by cronodragon
    In one of the Game Programming Gem books there is an article about creating a true randomizer
    In theory you can't make a true randomizer, but I suppose reading input etc. would make a pretty good aproximation
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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