Random Numbers and Chance

Discussion in 'ASP .Net' started by Chu, Mar 16, 2006.

  1. Chu

    Chu Guest

    I'm developing a game - in the game, the character has a 1 in 1000
    chance of finding a special weapon. The pseudo code idea is this:

    get a random number from 1 to 1000

    If random number is "1" then the user found a special weapon.

    My question is - will using .NET's buildin Random class really give me
    a true "1 in 1000" chance like I'd like as specified above? Are there
    any other ways that would be more precise?
     
    Chu, Mar 16, 2006
    #1
    1. Advertising

  2. Chu

    KBuser Guest

    the Random number generator uses the system clock to seed the
    generation... So if you have 10 calculations in less than a clock tick
    you will get the same 10 results.
     
    KBuser, Mar 16, 2006
    #2
    1. Advertising

  3. > My question is - will using .NET's buildin Random class really give me
    > a true "1 in 1000" chance like I'd like as specified above? Are there
    > any other ways that would be more precise?


    Basically, no random number generator on a conventional computer is
    "truly" random. The Random class uses an algorithm based on a seed
    (starting) value and modifying this to obtain the next "random" number.
    Hence, there is a predictable relation between one random number and
    the next, but this is not perceptible in conventional usage.

    The distribution of numbers throughout a range (in this case 1 - 1000)
    will be even on average, so you will get true 1/1000 chance. There is
    not really a more precise way, but as it is random, a slight variation
    is inherent, whatever method you use...
     
    Paul Henderson, Mar 16, 2006
    #3
  4. > the Random number generator uses the system clock to seed the
    > generation... So if you have 10 calculations in less than a clock tick
    > you will get the same 10 results.


    The Random number generator is *seeded* based on the system clock, but
    it should only be re-seeded when the program is started. Each number
    generated is a function of the previous, and so calling many times per
    unit time will not yield the same result. [Note I say "unit time"; a
    "tick" is not really correct as it refers to a single cycle, in which
    you could by definition call the RNG at most once]
     
    Paul Henderson, Mar 16, 2006
    #4
  5. Chu

    KBuser Guest

    Paul Henderson wrote:
    > > the Random number generator uses the system clock to seed the
    > > generation... So if you have 10 calculations in less than a clock tick
    > > you will get the same 10 results.

    >
    > The Random number generator is *seeded* based on the system clock, but
    > it should only be re-seeded when the program is started. Each number
    > generated is a function of the previous, and so calling many times per
    > unit time will not yield the same result. [Note I say "unit time"; a
    > "tick" is not really correct as it refers to a single cycle, in which
    > you could by definition call the RNG at most once]


    Interesting. Many months ago I was doing something similar to what he
    was doing, and I was getting the same number repeatedly output for a
    split second then it would change numbers and repeat that one about as
    many times. It was at that point somebody had explained to me the
    seeding issue, and told me that the number didn't get reseeded. I just
    created a simple program to test it, and it does appear to reseed it.
    I'm going to look for my old source code and see if I can't figure that
    out....
     
    KBuser, Mar 16, 2006
    #5
  6. Chu

    KBuser Guest

    I just wrote a program to randomly generate a number 0-99 50,000 times,
    it then counts the occurence of each number... Heres a sample of the
    output:
    The number 0 occurs 458 times.
    The number 1 occurs 496 times.
    The number 2 occurs 504 times.
    The number 3 occurs 485 times.
    The number 4 occurs 498 times.
    The number 5 occurs 511 times.
    The number 6 occurs 473 times.
    The number 7 occurs 462 times.
    The number 8 occurs 496 times.
    The number 9 occurs 503 times.
    The number 10 occurs 514 times.
    The number 11 occurs 477 times.
    The number 12 occurs 477 times.
    The number 13 occurs 498 times.
    The number 14 occurs 533 times.
    The number 15 occurs 514 times.
    The number 16 occurs 515 times.
    The number 17 occurs 492 times.
    The number 18 occurs 506 times.
    The number 19 occurs 534 times.
    The number 20 occurs 526 times.

    Not too shabby...
     
    KBuser, Mar 16, 2006
    #6
  7. Chu

    Chu Guest

    I plan to get around the seed problem by using a static class that
    seeds it the first time using the system clock. Every time after that I
    call the getRandom method it will be called on the same random object
    that was seeded when the program first started. This way, I shouldn't
    have to worry about getting duplicate random numbers sequentially that
    occur in the same clock tick.
     
    Chu, Mar 17, 2006
    #7
  8. Chu

    KBuser Guest

    Judging from my program's output, I don't think you have to... If you
    want the source just let me know.
     
    KBuser, Mar 17, 2006
    #8
  9. Chu

    Chu Guest

    In your program did you recreate a new random object or did you reuse
    the same one for all your number generation? If you use the same one,
    then you are basically doing the same as me by keeping the object
    static.
     
    Chu, Mar 17, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. globalrev
    Replies:
    4
    Views:
    782
    Gabriel Genellina
    Apr 20, 2008
  2. Alex Untitled
    Replies:
    11
    Views:
    670
    Giampiero Zanchi
    Nov 16, 2009
  3. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Feb 12, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    226
    PerlFAQ Server
    Feb 12, 2011
  4. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Apr 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    260
    PerlFAQ Server
    Apr 27, 2011
  5. VK
    Replies:
    15
    Views:
    1,211
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page