Reputation: 21
The return type is int
and the input parameters are also int
.
I am generating random integers in the while
loop and I am checking if that int
is the multiple of the input parameter int
and the return the number of tries it took to get that matched one.
And I want to do it in a while
loop and not in a for
loop.
public int findMul(int v){
double random = Math.random();
int numoftries = 0;
while(v % random == 0){
numoftries++;
}
return numoftries;
}
For some reason when I call it in the main method then execute it, it always shows the number 0 no matter what number and I don't know how to fix.
Upvotes: 0
Views: 321
Reputation: 31992
I'm going to suggest another way to do it.
When you generate the next number, you should only use rand.nextInt(v)
, because it guarantees that the generated number is smaller than v
. Generating a number greater than v
will never be a factor.
public int findMul(int v) {
Random rand = new Random();
int numoftries = 0;
int random = rand.nextInt(v);
while (true) {
if(random==0) {
numoftries++;
random=rand.nextInt(v);
continue;
}
if(v%random==0) {
numoftries++;
break;
}else {
numoftries++;
random=rand.nextInt(v);
}
}
return numoftries;
}
Your loop was originally returning 0
because the condition was v%random==0
, so if the first generated number wasn't a factor, it would instantaneously return numoftries
You also needed to generate a new number in the loop everytime, because then if the generated number wasn't a factor, it would be an infinite loop.
You can test the code like so:
import java.util.Random;
public class RandomFactor {
public int findMul(int v) {
Random rand = new Random();
int numoftries = 0;
int random = rand.nextInt(v);
while (true) {
if(random==0) {
numoftries++;
random=rand.nextInt(v);
continue;
}
if(v%random==0) {
numoftries++;
break;
}else {
numoftries++;
random=rand.nextInt(v);
}
}
return numoftries;
}
public static void main(String[] args) {
RandomFactor x = new RandomFactor();
System.out.println(x.findMul(1000));//1000 is the test case
}
}
Sometimes, if the number is too small, there is a higher possibility that it will generate 0
, which will throw a ArithmeticException. So we add an if statement to prevent that from happening.
If you want to use nextInt()
to achieve the same effect:
import java.util.Random;
import java.util.Scanner;
public class RandomFactor {
public int findMul(int v) {
Random rand = new Random();
int numoftries = 0;
int random = rand.nextInt(v);
while (true) {
if(random==0) {
numoftries++;
continue;
}
if(v%random==0) {
numoftries++;
break;
}else {
numoftries++;
random=rand.nextInt(v);
}
}
return numoftries;
}
public static void main(String[] args) {
RandomFactor x = new RandomFactor();
Scanner input = new Scanner(System.in);
while(true) {
try {
System.out.print("\nEnter your guess: ");
int guess = input.nextInt();
System.out.println(x.findMul(guess));
}catch(NumberFormatException e) {
break;
}
}
}
}
If you must use Math.random
import java.util.Scanner;
public class RandomFactor {
public static int digits(int number) {
String current = String.valueOf(number);
return current.length();
}
public static int newrandom(int v) {
return (int)(Math.random()*(digits(v)*10));
}
public int findMul(int v) {
int random = newrandom(v);
int numoftries = 0;
while (true) {
if(random==0) {
numoftries++;
random = newrandom(v);
continue;
}
if(v%random==0) {
numoftries++;
break;
}else {
numoftries++;
random=newrandom(v);
}
}
return numoftries;
}
public static void main(String[] args) {
RandomFactor x = new RandomFactor();
Scanner input = new Scanner(System.in);
while(true) {
try {
System.out.print("\nEnter your guess: ");
int guess = input.nextInt();
System.out.println(x.findMul(guess));
}catch(NumberFormatException e) {
break;
}
}
}
}
Explanation
Because Math.random returns a double, we need to generate a number with the same number of digits as the guess, and then cast to an integer so it can compare properly. We do this by converting it to a String (method digits) and then multiplying the result by 10 as Math.random returns a number between 0 and 1. However, keep in mind that this is not efficient, as the more digits the number has, the more guess it will make. For example, if we guess 5, it will guess from 0-9. If we guess 10, it will guess from 10-100.
EDIT: Fixed a fatal bug which causes it to loop forever, because if the random numuber was 0
, it doesn't re-generate a number.
Upvotes: 1