Last active
August 29, 2015 14:26
-
-
Save tprado/8bbdfd6f5177ec08a7fd to your computer and use it in GitHub Desktop.
java random spike
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class RandomSpike { | |
private static final int PERCENTAGE = 50; | |
public static void main(String[] args) { | |
intNext100Example(); | |
intNext100KExample(); | |
doubleExample(); | |
} | |
private static void intNext100Example() { | |
int a = 0; | |
int b = 0; | |
Random r = new Random(); | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < Integer.MAX_VALUE; i++) { | |
if (r.nextInt(100) > PERCENTAGE) { | |
a++; | |
} else { | |
b++; | |
} | |
} | |
System.out.println("nextInt(100):"); | |
System.out.println("a=" + a); | |
System.out.println("b=" + b); | |
System.out.println("t=" + (System.currentTimeMillis() - start)); | |
} | |
private static void intNext100KExample() { | |
int a = 0; | |
int b = 0; | |
Random r = new Random(); | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < Integer.MAX_VALUE; i++) { | |
if (r.nextInt(100000) > PERCENTAGE * 1000) { | |
a++; | |
} else { | |
b++; | |
} | |
} | |
System.out.println("nextInt(100K):"); | |
System.out.println("a=" + a); | |
System.out.println("b=" + b); | |
System.out.println("t=" + (System.currentTimeMillis() - start)); | |
} | |
private static void doubleExample() { | |
int a = 0; | |
int b = 0; | |
Random r = new Random(); | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < Integer.MAX_VALUE; i++) { | |
if (r.nextDouble() > (double) PERCENTAGE / 100) { | |
a++; | |
} else { | |
b++; | |
} | |
} | |
System.out.println("nextDouble:"); | |
System.out.println("a=" + a); | |
System.out.println("b=" + b); | |
System.out.println("t=" + (System.currentTimeMillis() - start)); | |
} | |
} |
Author
tprado
commented
Jul 27, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment