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.
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.
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.
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.
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.
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)
Posts
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.
Street Fighter 4 (pc): sdurien
Steam: Jon http://steamcommunity.com//profiles/76561197970923897/home
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)
Street Fighter 4 (pc): sdurien
Steam: Jon http://steamcommunity.com//profiles/76561197970923897/home
Math.random() seeds itself only once, the first time you use it. It creates a single (static) instance for your application to use.
Street Fighter 4 (pc): sdurien
Steam: Jon http://steamcommunity.com//profiles/76561197970923897/home