The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

java random number generator question.

NaeblissNaebliss Registered User regular
edited April 2008 in Help / Advice Forum
Hello pa. I am writing a java program for my group project in probability. Our problem is "the birthday problem": what is the probability of two kids having the same birthday in a class of n students? I'm using the java.util.Random class to generate the students birthdays. My question to you is; if i make a new Random object for each student, as opposed to making one for the class(school class not java class :)) and then getting an integer for each student, will my results be effected? I have done it both ways and my results seem consistently lower than the theoretical if i make a new object for each student and they seem dead on when i make 1 for each class, but I don't understand why. thanks.

This couch is the comfiest!
Street Fighter 4 (pc): sdurien
Steam: Jon http://steamcommunity.com//profiles/76561197970923897/home
Naebliss on

Posts

  • jotatejotate Registered User regular
    edited April 2008
    Are you re-seeding with the same number for each student?

    jotate on
  • falsedeffalsedef Registered User regular
    edited April 2008
    Instance as many Random classes as you need, never more (one is usually enough). And never instance them with the same seeds or close in time.

    It's best to just use Math.random() rather than instancing your own.

    Basically if you make a new Random instance too quickly, it'll get seeded with the time as the last random. Windows timings are in the 20ms range, which means a great overlap of seed values for any Random created within that time.

    falsedef on
  • NaeblissNaebliss Registered User regular
    edited April 2008
    jotate wrote: »
    Are you re-seeding with the same number for each student?
    no i left the constructor's parameters blank. like so: Random generator = new Random();. I believe this uses the system clock to seed the generator. initially I made a new Random object for each student but now I am making one for each class. I'm not sure how i can go about making a single generator for the main method, because I need a new random integer(1-365) for each student. I guess I could make a generator for the main method and use a generated integer as a parameter for my class Class and seed the class generator with that integer. If that makes any sense. D:

    Naebliss on
    This couch is the comfiest!
    Street Fighter 4 (pc): sdurien
    Steam: Jon http://steamcommunity.com//profiles/76561197970923897/home
  • falsedeffalsedef Registered User regular
    edited April 2008
    Naebliss wrote: »
    no i left the constructor's parameters blank. like so: Random generator = new Random();. I believe this uses the system clock to seed the generator. initially I made a new Random object for each student but now I am making one for each class. I'm not sure how i can go about making a single generator for the main method, because I need a new random integer(1-365) for each student. I guess I could make a generator for the main method and use a generated integer as a parameter for my class Class and seed the class generator with that integer. If that makes any sense. D:

    Dude, that's exactly what you shouldn't do.

    Just use Math.random() if you don't know how to use statics.

    (int)(Math.random()*n) will return an integer [0,n)

    falsedef on
  • NaeblissNaebliss Registered User regular
    edited April 2008
    If I use math.random() inside my student constructor? will it reseed with every student? or will that work ok?

    Naebliss on
    This couch is the comfiest!
    Street Fighter 4 (pc): sdurien
    Steam: Jon http://steamcommunity.com//profiles/76561197970923897/home
  • falsedeffalsedef Registered User regular
    edited April 2008
    Naebliss wrote: »
    If I use math.random() inside my student constructor? will it reseed with every student? or will that work ok?

    Math.random() seeds itself only once, the first time you use it. It creates a single (static) instance for your application to use.

    falsedef on
  • NaeblissNaebliss Registered User regular
    edited April 2008
    Ok I'll try it out. thanks for the help all.

    Naebliss on
    This couch is the comfiest!
    Street Fighter 4 (pc): sdurien
    Steam: Jon http://steamcommunity.com//profiles/76561197970923897/home
Sign In or Register to comment.