Getting same random numbers every time - java

I'm trying to write a program that generates a random school timetable with random positions and a random amount of hours for each teacher, but with a fixed total amount of time per day. For now the program is written to work with two days, and I'm encountering an issue: the random-generated values for the amount of time between the two days are the same:
import java.util.Random;
public class randomTimetable {
public static void main(String[] args) {
String newLine = System.getProperty("line.separator");
System.out.println("For each day (x + y + ... n) >= 5 and" +newLine +"(x && y && ... n) <= 2" +newLine);
createTimetable();
}
private static void createTimetable() {
String x_g1 = "x";
String y_g1 = "y";
String z_g1 = "z";
String m_g1 = "m";
String[] arrayTimetablePosition1={x_g1, y_g1, z_g1, m_g1};
String newLine = System.getProperty("line.separator");
System.out.println("Work In Progress" +newLine +"Total subjects = 5" +newLine +"Day 1");
Random rand = new Random();
int min = 0;
int max = 2;
int x1 = rand.nextInt(max - min + 1) + min;
int y1 = rand.nextInt(max - min + 1) + min;
int z1 = rand.nextInt(max - min + 1) + min;
int m1 = rand.nextInt(max - min + 1) + min;
while((x1 + y1 + z1 + m1) != 5) {
x1 = rand.nextInt(max - min + 1) + min;
y1 = rand.nextInt(max - min + 1) + min;
z1 = rand.nextInt(max - min + 1) + min;
m1 = rand.nextInt(max - min + 1) + min;
}
System.out.println("x1 = " +x1 +newLine +"y1 = " +y1 +newLine +"z1 = " +z1 +newLine +"m1 = " +m1 +newLine);
System.out.println("Total subjects = 5" +newLine +"Day 2");
int x2 = rand.nextInt(max - min + 1) + min;
int y2 = rand.nextInt(max - min + 1) + min;
int z2 = rand.nextInt(max - min + 1) + min;
int m2 = rand.nextInt(max - min + 1) + min;
while((x2 + y2 + z2 + m2) != 5 && (x1 == x2 || y1 == y2 || z1 == z2 || m1 == m2)) {
x2 = rand.nextInt(max - min + 1) + min;
y2 = rand.nextInt(max - min + 1) + min;
z2 = rand.nextInt(max - min + 1) + min;
m2 = rand.nextInt(max - min + 1) + min;
}
System.out.println("x2 = " +x1 +newLine +"y2 = " +y1 +newLine +"z2 = " +z1 +newLine +"m2 = " +m1 +newLine);
}
}
specifically the value of x1 is the same of x2, the one of y1 is the same of y2 and so on.

You're Random construction is fine - you're using the default constructor, which automatically uses the time as a seed:
public Random() { this(System.currentTimeMillis()); }
But you have a copy/paste error in your last debug-print statement. You're label says x2, but you're printing x1, etc.
System.out.println("x2 = " +x1 +newLine +"y2 = " +y1 +newLine +"z2 = " +z1 +newLine +"m2 = " +m1 +newLine);

I cannot see any initialization of the pseudorandom number generator.
You need to set the seed of the PRNG.

Looks like you are using the same seed. see:
http://docs.oracle.com/javase/6/docs/api/java/util/Random.html

LOL, it's a copy-paste error
last line should read
System.out.println("x2 = " +x2 +newLine +"y2 = " +y2 +newLine +"z2 = " +z2 +newLine +"m2 = " +m2 +newLine);
but it sounds like a classic random-seed problem. That's pretty funny.

Related

how to split number into pieces with remaining part

I want to split any number to any identical pieces and the last remaining but not dividable piece will be the last piece. I wrote this code but I know that it should be more simple way to do this :) For example; 7500 divided by 2000 and the last modulus part will be the last part. Any suggestions?
public class MyClass {
public static void main(String args[]) {
int x =7500;
int y = 2000;
int lastPartCount = 0;
String result = new String();
if(x%y != 0){
lastPartCount = x%y;
}
int newx = x-lastPartCount;
for(int i=1; i<=(newx/y); i++){
if(i == 1){//first member
result = "part " + i + ": 0-" + y*i;
}else
{
result = "part " + i + ": " + (y*(i-1)) + "-" + y*i;
}
System.out.println(result);
if(i == (newx/y)){//last member
result = "part " + (i+1) + ": " + (y*(i)) + "-" + x;
System.out.println(result);
}
}
}
}
the result is like this:
part 1: 0-2000
part 2: 2000-4000
part 3: 4000-6000
part 4: 6000-7500
You can simplify your code like the following:
public static void main(String args[]) {
int x = 7500;
int y = 2000;
for (int i = 0; i < x/y; i++) {
System.out.println("Part " + (i+1) + ": " + y*i + " - " + y*(i+1));
}
if (x%y != 0) {
System.out.println("Part " + ((x/y)+1) + ": " + (x/y)*y + " - " + x);
}
}
(x/y)*y) is not equal to x since you divide integers, so (x/y)*y is actually the same as the "next" i of the for-loop.
You can also try the below code:
private void test() {
int x = 7500;
int y = 2000;
int j = 0;
int newX = x;
while (newX > y) {
System.out.println("Part " + (j + 1) + " = " + y * j++ + " - " + y * j);
newX -= y;
}
System.out.println("Part " + (j + 1) + " = " + j * y + " - " + x);
}
Alternative approach using two variables in your for loop and Math.min():
int x = 7500;
int y = 2000;
for (int i = 0, p = 1; i < x; i += y, p++) {
System.out.printf("Part %d: %d - %d%n", p, i, Math.min(i+y,x));
}

my "Yahtzee" code sometimes throws in a random number

Alright so basically I wanted to make a code that would generate numbers until it hit a perfect "Yahtzee" first try. Sometimes it doesn't work and I'm pretty new to coding (which is why this is an abomination) and even newer to SO. I did this just for education but I don't understand why it sometimes doesn't work. About 20% of the time, instead of saying "11111" it will say "11114" randomly. Help me out?
public class IDontKnowWhatImDoing {
public static void main(String[] args) {
int min = 1;
int max = 6;
int randomInt1 = (int)(Math.random() * (max - min + 1) + min);
int randomInt2 = (int)(Math.random() * (max - min + 1) + min);
int randomInt3 = (int)(Math.random() * (max - min + 1) + min);
int randomInt4 = (int)(Math.random() * (max - min + 1) + min);
int randomInt5 = (int)(Math.random() * (max - min + 1) + min);
while(randomInt1 != randomInt2) {
randomInt1 = (int)(Math.random() * (max - min + 1) + min);
randomInt2 = (int)(Math.random() * (max - min + 1) + min);
randomInt3 = (int)(Math.random() * (max - min + 1) + min);
randomInt4 = (int)(Math.random() * (max - min + 1) + min);
randomInt5 = (int)(Math.random() * (max - min + 1) + min);
System.out.println(randomInt1);
System.out.println(randomInt2);
System.out.println(randomInt3);
System.out.println(randomInt4);
System.out.println(randomInt5);
System.out.println();
while(randomInt2 != randomInt3) {
randomInt1 = (int)(Math.random() * (max - min + 1) + min);
randomInt2 = (int)(Math.random() * (max - min + 1) + min);
randomInt3 = (int)(Math.random() * (max - min + 1) + min);
randomInt4 = (int)(Math.random() * (max - min + 1) + min);
randomInt5 = (int)(Math.random() * (max - min + 1) + min);
System.out.println(randomInt1);
System.out.println(randomInt2);
System.out.println(randomInt3);
System.out.println(randomInt4);
System.out.println(randomInt5);
System.out.println();
while(randomInt3 != randomInt4) {
randomInt1 = (int)(Math.random() * (max - min + 1) + min);
randomInt2 = (int)(Math.random() * (max - min + 1) + min);
randomInt3 = (int)(Math.random() * (max - min + 1) + min);
randomInt4 = (int)(Math.random() * (max - min + 1) + min);
randomInt5 = (int)(Math.random() * (max - min + 1) + min);
System.out.println(randomInt1);
System.out.println(randomInt2);
System.out.println(randomInt3);
System.out.println(randomInt4);
System.out.println(randomInt5);
System.out.println();
while(randomInt4 != randomInt5) {
randomInt1 = (int)(Math.random() * (max - min + 1) + min);
randomInt2 = (int)(Math.random() * (max - min + 1) + min);
randomInt3 = (int)(Math.random() * (max - min + 1) + min);
randomInt4 = (int)(Math.random() * (max - min + 1) + min);
randomInt5 = (int)(Math.random() * (max - min + 1) + min);
System.out.println(randomInt1);
System.out.println(randomInt2);
System.out.println(randomInt3);
System.out.println(randomInt4);
System.out.println(randomInt5);
System.out.println();
}
}
}
}
}
}
The problem is in the conditions you use to check whether you have a five of the same number. If a certain condition is already false (the numbers match), you don't enter that look.
For example, if you randomly generate five numbers, where the first two happen to be equal, then randomInt1 != randomInt2 will be false.
You will then never enter the while(randomInt1 != randomInt2) block, and exit with that initial set of numbers, even though you've only checked that the first two are equal, not the other 3. This problem is present at each level of your nested loops.
The direct solution would be to use do-while loops instead (and removing the first number generator block). This allows each pair of numbers to actually be checked at least once.
That is however an inelegant solution. Assuming that you still want to generate five numbers each time (as in Yahtzee, instead of something simpler like generating one die at a time), it would be cleaner to have a single do-while loop. In that, you could have a single block of random number calls instead of redundant ones at different nesting levels. The condition would have it continue looping until all 5 are equal.
You really don't need all that repetitious code. It all boils down to the condition you use for your while loop. You already know that you need 5 of a kind for a Yahtzee and that means that all dice must match the first dice that is rolled so, your while loop
condition can be:
while ((randomInt1 != randomInt2) || (randomInt1 != randomInt3) ||
(randomInt1 != randomInt4) || (randomInt1 != randomInt5)) {
// ........ other code ....
}
This condition keeps the loop running until all dice are of the same value. With this in mind, you can reduce your code to:
int min = 1;
int max = 6;
int randomInt1 = 1;
int randomInt2 = 2;
int randomInt3 = 3;
int randomInt4 = 4;
int randomInt5 = 5;
while ((randomInt1 != randomInt2) || (randomInt1 != randomInt3) ||
(randomInt1 != randomInt4) || (randomInt1 != randomInt5)) {
randomInt1 = (int) (Math.random() * (max - min + 1) + min);
randomInt2 = (int) (Math.random() * (max - min + 1) + min);
randomInt3 = (int) (Math.random() * (max - min + 1) + min);
randomInt4 = (int) (Math.random() * (max - min + 1) + min);
randomInt5 = (int) (Math.random() * (max - min + 1) + min);
System.out.print(randomInt1 + " " + randomInt2 + " " +
randomInt3 + " " + randomInt4 + " " + randomInt5);
System.out.println();
}
System.out.println(" YAHTZEE");
You can however reduce more repetitious code. You really only need one random number generator. By making your dice an array of dice, for example:
int[] dice = new int[5];
boolean yahtzee = false;
while (!yahtzee) {
int count = 1;
for (int i = 0; i < dice.length; i++) {
dice[i] = (int) (Math.random() * (6 - 1 + 1) + 1);
if (i > 0 && dice[i] == dice[i-1]) {
count++;
}
}
if (count == 5) {
yahtzee = true;
}
}
System.out.println(Arrays.toString(dice) + " YAHTZEE!!");
Another way might be by making use of a HashSet since a HashSet will only hold distinct values, for example:
Integer[] dice = new Integer[5];
java.util.Set<Integer> s = new java.util.HashSet<>();
// If all elements are same, size of HashSet should be 1.
// since the HashSet contains only 'distinct' values.
while (s.size() != 1) {
for (int i = 0; i < dice.length; i++) {
dice[i] = (int) (Math.random() * (6 - 1 + 1) + 1);
}
// Put all array elements in a HashSet
s = new java.util.HashSet<>(Arrays.asList(dice));
}
System.out.println(Arrays.toString(dice) + " YAHTZEE!!");

How can I get the result value in java overloading

I want to write a program such that if the input is true, it adds a and b, and if the input is false, it subtracts b from a. Also, when it is an ArrayList, if the input is true, it picks the maximum value, and if the input is false, it picks the minimum value.
public class Source7_3 {
public static void main(String[] args) {
OverLoading mm = new OverLoading();
int[] a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = (int) (Math.random() * 100) + 1;
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = ");
System.out.println("dist(arr, " + true + ") = ");
System.out.println("dist(arr, " + false + ") = ");
}
}
class OverLoading {
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int[] a, boolean d) {
for (int j = 0; j < a.length; j++) {
int max, min;
max = min = a[0];
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
return true ? max : min;
}
}
}
But I can't get the result value..
How can I get it?
Thank you for your help!
I think you are trying to call these methods but at the moment you are simply appending Strings
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = ");
should maybe be
System.out.println(mm.dist(mm.a, mm.b, true);
and as the fields a and b are part of the class, it would not be necessary to pass them
class OverLoading
{
int a = (int) (Math.random() * 100) + 1;
int b = (int) (Math.random() * 100) + 1;
int dist(int a, int b, boolean d) {
return d == true ? a + b : a - b;
}
int dist(int[] a, boolean d) {
int max, min;
max = min = a[0];
for (int j = 0; j < a.length; j++) {
if (max < a[j])
max = a[j];
if (min > a[j])
min = a[j];
}
return d == true ? max : min;
}
}
public class HelloWorld{
public static void main(String[] args) {
OverLoading mm = new OverLoading();
int[] a = new int[10];
System.out.println("\n");
for (int i = 0; i < a.length; i++)
{
a[i] = (int) (Math.random() * 100) + 1;
System.out.println(a[i]);
}
System.out.println("\n");
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + true + ") = "+mm.dist(mm.a,mm.b,true));
System.out.println("dist(" + mm.a + ", " + mm.b + ", " + false + ") = "+mm.dist(mm.a,mm.b,false));
System.out.println("dist(arr, " + true + ") = "+mm.dist(a,true));
System.out.println("dist(arr, " + false + ") = "+mm.dist(a,false));
}
}

Math in Java(Combinatorics)

My problem is:
My math formula is:
In this case X = N; Y = L;U = K;
public class Play {
public static void main(String args[]) {
//n!(n−k−1)!
int n = 10;
int k =2;
int l = 12;
long result;
result = (calculaFator(n) / calculaFator(n-k-1));
result= (long) (result * Math.pow((n-k),(l-k)-1));
System.out.println(result);
}
public static long calculaFator(long x) {
long f = x;
while (x > 1) {
f = f * (x - 1);
x--;
}
return f;
}
}
It should be 721599986, but it is giving me 96636764160
I have some samples:
With n=10, k=2, l=12 it should be 721599986
With n=10, k=2, l=16 it should be 626284798
With n=10, k=1, l=20 it should be 674941304
With n=5, k=2, l=8 it should be 10800
The java codes is working according to your stated formula.
It seems like the formula is wrong rather than the codes. (or expected results or your x,u,y mapping to n,l,k is incorrect?)
int x = 10;
int u = 2;
int y = 12;
long numerator = calculaFator(x);
long denominator = calculaFator(x - u - 1);
int xu1 = x - u - 1;
long result = numerator / denominator;
System.out.println();
System.out.println(x + "!= numerator: " + numerator); //10!= numerator: 3_628_800
System.out.println(xu1 + "!= denominator: " + denominator); //7!= denominator: 5_040
System.out.println("result1: " + result); //result1: 720 (correct)
int xu = x - u;
int yu1 = y - u - 1;
double remainderPlaylist = Math.pow(xu, yu1);
System.out.println(xu + "^" + yu1 + " = " + remainderPlaylist);//8^9 = 1.34217728E8
System.out.println(xu + "^" + yu1 + " = " + (long) remainderPlaylist);//8^9 = 134_217_728 (correct)
long mul = (long) (result * remainderPlaylist);
System.out.println(result + "x" + (long)remainderPlaylist + " = " + mul); //720x134_217_728 = 96_636_764_160 (mathematically correct)

Fill a 2-D array with all the factors of the numbers 1 to 1000 - java

I'm attempting to fill a 2D array with the numbers 1 to 1000 and then show all the factors of those numbers. I then need to find all the prime numbers in the same array and output them. Here's what I have so far, keep in mind that I was hoping to do every step in its own method then return them but have not got that far yet
int i = 0;
//int x = 0;
String primeNumber = "";
int[] [] factorArray = new int [1000] [];
for (int x = 0 ; x < 1000 ; x++)
{
int remainder;
int y;
remainder = x % 2;
y = x / 2;
if (remainder != 0)
System.out.println (x + ": " + "1, " + x);
else if (remainder == 0)
System.out.println (x + ": " + (y) + " , " + (y / 2) + " , " + " 1, " + x);
}
for (i = 1 ; i <= 1000 ; i++)
{
int ctr = 0;
for (int x = i ; x >= 1 ; x--)
{
if (i % x == 0){
ctr = ctr + 1;
}
}
if (ctr == 2)
{
primeNumber = primeNumber + i + " ";
}
}
System.out.print ("Prime numbers from 1 - 1000 are : \n" + primeNumber);

Categories