Random Numbers and Java.
Discussion
My programming in Java is a bit rusty, but I am having problems generating reasonable random numbers using java.util.Random.
Using the following :
Random generator = new Random();
double r = generator.nextDouble() * 1000;
to generate a random number up to 1000, however repeatedly testing this shows that it isn't really random at all, even if including a seed.
Anyone got any ideas on how to generate close to true random numbers as possible?
Using the following :
Random generator = new Random();
double r = generator.nextDouble() * 1000;
to generate a random number up to 1000, however repeatedly testing this shows that it isn't really random at all, even if including a seed.
Anyone got any ideas on how to generate close to true random numbers as possible?
Plenty of opensource mathematically decent random-number algorithms available, I'd just find an algorithm that's well regarded and code it up into my own class (I'm not a Java programmer BTW, but this sort of thing really can't be more than a few tens of lines of code, surely).
Then the key is to seed the generator with a value that's likely to have a high level of natural randomness. If it has to be easily available then a system statistic such as current CPU usage or network packets sent in last second could be used, I suppose. Obviously you'll have to choose the most naturally variable statistic knowing your architecture, but you should be able to achieve a pretty decent level of randomness with a generator using these principles, no?
The gold standard is probably going to be EM radiation from space picked up by radio telescopes, so you could subvert some of SETI's work units for this job
Then the key is to seed the generator with a value that's likely to have a high level of natural randomness. If it has to be easily available then a system statistic such as current CPU usage or network packets sent in last second could be used, I suppose. Obviously you'll have to choose the most naturally variable statistic knowing your architecture, but you should be able to achieve a pretty decent level of randomness with a generator using these principles, no?
The gold standard is probably going to be EM radiation from space picked up by radio telescopes, so you could subvert some of SETI's work units for this job

With...
Random generator = new Random();
for(int i = 0; i < 10; i++) {
double r = generator.nextDouble() * 1000;
System.out.println("r <" + r + ">"
;
}
...I get this on the first run...
r <146.50167553034976>
r <481.16917924659697>
r <291.45743428145386>
r <642.5893636276335>
r <376.24867311910936>
r <699.66028342995>
r <139.6328525309497>
r <645.5333400126177>
r <460.4322739733999>
r <716.2185713078283>
...and this on the second run...
r <386.1674743637572>
r <365.3458395155518>
r <399.04890604373713>
r <480.94358315480633>
r <73.20830696272529>
r <369.18363349574554>
r <861.6576167336215>
r <981.7290766140208>
r <768.7126736306881>
r <812.6843188715297>
...how random do you need it to be as this seems to be working ok?
Mark
Random generator = new Random();
for(int i = 0; i < 10; i++) {
double r = generator.nextDouble() * 1000;
System.out.println("r <" + r + ">"
;}
...I get this on the first run...
r <146.50167553034976>
r <481.16917924659697>
r <291.45743428145386>
r <642.5893636276335>
r <376.24867311910936>
r <699.66028342995>
r <139.6328525309497>
r <645.5333400126177>
r <460.4322739733999>
r <716.2185713078283>
...and this on the second run...
r <386.1674743637572>
r <365.3458395155518>
r <399.04890604373713>
r <480.94358315480633>
r <73.20830696272529>
r <369.18363349574554>
r <861.6576167336215>
r <981.7290766140208>
r <768.7126736306881>
r <812.6843188715297>
...how random do you need it to be as this seems to be working ok?
Mark
dern said:
With...
Random generator = new Random();
for(int i = 0; i < 10; i++) {
double r = generator.nextDouble() * 1000;
System.out.println("r <" + r + ">"
;
}
...I get this on the first run...
r <146.50167553034976>
r <481.16917924659697>
r <291.45743428145386>
r <642.5893636276335>
r <376.24867311910936>
r <699.66028342995>
r <139.6328525309497>
r <645.5333400126177>
r <460.4322739733999>
r <716.2185713078283>
...and this on the second run...
r <386.1674743637572>
r <365.3458395155518>
r <399.04890604373713>
r <480.94358315480633>
r <73.20830696272529>
r <369.18363349574554>
r <861.6576167336215>
r <981.7290766140208>
r <768.7126736306881>
r <812.6843188715297>
...how random do you need it to be as this seems to be working ok?
Mark
I get similar to that, but I'm trying to simulate a random event occuring multiple times - so, for example, in real life it should have a probability of occuring 1/100 times, but done using Java it comes back as 20,000 out of 100,000 times (or 20/100).Random generator = new Random();
for(int i = 0; i < 10; i++) {
double r = generator.nextDouble() * 1000;
System.out.println("r <" + r + ">"
;}
...I get this on the first run...
r <146.50167553034976>
r <481.16917924659697>
r <291.45743428145386>
r <642.5893636276335>
r <376.24867311910936>
r <699.66028342995>
r <139.6328525309497>
r <645.5333400126177>
r <460.4322739733999>
r <716.2185713078283>
...and this on the second run...
r <386.1674743637572>
r <365.3458395155518>
r <399.04890604373713>
r <480.94358315480633>
r <73.20830696272529>
r <369.18363349574554>
r <861.6576167336215>
r <981.7290766140208>
r <768.7126736306881>
r <812.6843188715297>
...how random do you need it to be as this seems to be working ok?
Mark
kiwisr said:
I get similar to that, but I'm trying to simulate a random event occuring multiple times - so, for example, in real life it should have a probability of occuring 1/100 times, but done using Java it comes back as 20,000 out of 100,000 times (or 20/100).
Can you run this (I can't figure out the formatting so you'll have to figure it out I'm afraid)...import java.util.Random;
public class MyRandom {
public static void main(String[] args) {
for(int i = 0; i < 100; i++) {
int count = 0;
Random generator = new Random();
for(int j = 0; j < 100000; j++) {
double r = generator.nextDouble() * 100;
if(r < 1) {
count++;
}
}
System.out.println("iteration <" + (i + 1) + "> - count <" + count + "/100000>" );
}
}
}
...because for me it gives this...
iteration <1> - count <979/100000>
iteration <2> - count <990/100000>
iteration <3> - count <1065/100000>
iteration <4> - count <971/100000>
iteration <5> - count <1014/100000>
iteration <6> - count <970/100000>
iteration <7> - count <973/100000>
iteration <8> - count <1014/100000>
iteration <9> - count <1003/100000>
iteration <10> - count <961/100000>
iteration <11> - count <932/100000>
iteration <12> - count <1008/100000>
iteration <13> - count <1023/100000>
iteration <14> - count <1006/100000>
iteration <15> - count <975/100000>
iteration <16> - count <990/100000>
iteration <17> - count <1000/100000>
iteration <18> - count <956/100000>
iteration <19> - count <996/100000>
iteration <20> - count <989/100000>
iteration <21> - count <1052/100000>
iteration <22> - count <983/100000>
iteration <23> - count <1014/100000>
iteration <24> - count <1051/100000>
iteration <25> - count <1000/100000>
iteration <26> - count <994/100000>
iteration <27> - count <996/100000>
iteration <28> - count <1022/100000>
iteration <29> - count <1068/100000>
iteration <30> - count <1041/100000>
iteration <31> - count <962/100000>
iteration <32> - count <1017/100000>
iteration <33> - count <953/100000>
iteration <34> - count <997/100000>
iteration <35> - count <996/100000>
iteration <36> - count <963/100000>
iteration <37> - count <1013/100000>
iteration <38> - count <994/100000>
iteration <39> - count <1014/100000>
iteration <40> - count <1005/100000>
iteration <41> - count <1012/100000>
iteration <42> - count <1021/100000>
iteration <43> - count <998/100000>
iteration <44> - count <964/100000>
iteration <45> - count <963/100000>
iteration <46> - count <990/100000>
iteration <47> - count <1021/100000>
iteration <48> - count <1024/100000>
iteration <49> - count <1023/100000>
iteration <50> - count <960/100000>
iteration <51> - count <987/100000>
iteration <52> - count <961/100000>
iteration <53> - count <1022/100000>
iteration <54> - count <921/100000>
iteration <55> - count <949/100000>
iteration <56> - count <1003/100000>
iteration <57> - count <1005/100000>
iteration <58> - count <981/100000>
iteration <59> - count <965/100000>
iteration <60> - count <985/100000>
iteration <61> - count <1013/100000>
iteration <62> - count <940/100000>
iteration <63> - count <1030/100000>
iteration <64> - count <963/100000>
iteration <65> - count <997/100000>
iteration <66> - count <970/100000>
iteration <67> - count <1015/100000>
iteration <68> - count <966/100000>
iteration <69> - count <1015/100000>
iteration <70> - count <968/100000>
iteration <71> - count <961/100000>
iteration <72> - count <999/100000>
iteration <73> - count <972/100000>
iteration <74> - count <986/100000>
iteration <75> - count <987/100000>
iteration <76> - count <941/100000>
iteration <77> - count <1000/100000>
iteration <78> - count <1023/100000>
iteration <79> - count <1012/100000>
iteration <80> - count <977/100000>
iteration <81> - count <961/100000>
iteration <82> - count <971/100000>
iteration <83> - count <981/100000>
iteration <84> - count <1034/100000>
iteration <85> - count <931/100000>
iteration <86> - count <978/100000>
iteration <87> - count <1003/100000>
iteration <88> - count <970/100000>
iteration <89> - count <1021/100000>
iteration <90> - count <942/100000>
iteration <91> - count <971/100000>
iteration <92> - count <966/100000>
iteration <93> - count <1006/100000>
iteration <94> - count <1039/100000>
iteration <95> - count <1032/100000>
iteration <96> - count <1079/100000>
iteration <97> - count <971/100000>
iteration <98> - count <990/100000>
iteration <99> - count <1035/100000>
iteration <100> - count <1019/100000>
...which I think is pretty much what you want isn't it as the count should approximate 1000 which is 1% of 100000?
If you don't get the same as me then I think there's something wrong with your copy of Java and if you do I'd re-examine your own code.
Mark
Edited by dern on Tuesday 23 October 17:00
dern said:
kiwisr said:
I get similar to that, but I'm trying to simulate a random event occuring multiple times - so, for example, in real life it should have a probability of occuring 1/100 times, but done using Java it comes back as 20,000 out of 100,000 times (or 20/100).
Can you run this (I can't figure out the formatting so you'll have to figure it out I'm afraid)...import java.util.Random;
public class MyRandom {
public static void main(String[] args) {
for(int i = 0; i < 100; i++) {
int count = 0;
Random generator = new Random();
for(int j = 0; j < 100000; j++) {
double r = generator.nextDouble() * 100;
if(r < 1) {
count++;
}
}
System.out.println("iteration <" + (i + 1) + "> - count <" + count + "/100000>" );
}
}
}
for(int j = 0; j < 100000; j++) {
Random generator = new Random();
double r = generator.nextDouble() * 100;
rather than :
Random generator = new Random();
for(int j = 0; j < 100000; j++) {
double r = generator.nextDouble() * 100;
kiwisr said:
Thanks, that helped. I was using :
for(int j = 0; j < 100000; j++) {
Random generator = new Random();
double r = generator.nextDouble() * 100;
rather than :
Random generator = new Random();
for(int j = 0; j < 100000; j++) {
double r = generator.nextDouble() * 100;
Ah. That's because the constructor Random() is actually implemented public Random() { this(System.currentTimeMillis()); } so conceivably you could be starting a new random sequence with the same seed for a number of consecutive iterations (resulting in the same result from generator.nextDouble()) and from what you've said this may be 20 iterations before a millisecond passed and a new seed was used. Glad to have been able to help.for(int j = 0; j < 100000; j++) {
Random generator = new Random();
double r = generator.nextDouble() * 100;
rather than :
Random generator = new Random();
for(int j = 0; j < 100000; j++) {
double r = generator.nextDouble() * 100;
Edited by dern on Tuesday 23 October 21:03
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


