I need to generate a random number between -99 to 99, how do I code this? This is what I have so far, but it always comes out negative
Random gererator=new Random();
num1=Math.abs(generator.nextInt()) % 99 + -99;
How do I fix it?
Printable View
I need to generate a random number between -99 to 99, how do I code this? This is what I have so far, but it always comes out negative
Random gererator=new Random();
num1=Math.abs(generator.nextInt()) % 99 + -99;
How do I fix it?
this is not a fix for your code but it is how i would do it
//this generates a random 0 or 1
int b = (int) (Math.random()*100%2);
//this generates a random number from 0 to 99
int a = (int) (Math.random()*100%100);
//this is how i turn it to a negative
//but only when the random numberb is 0
if(b==0){ a= a-(a*2);}
//this prints a or -a (if you understand)
System.out.println (a);
hope it helps sorry if i have english trouble its not mi native languaje
...or you could generate a random number between 0 and 99 and then make another random decision as to whether it should be negative or not...