generating decimals

i want to generate random numbers that allways have 3 digits:

123 or 1,23 or 12,3

so far i'm unable to:

class Multiplicar{

// gerar n齧eros

int f1 = (int)(Math.random() * 100);

int f2 = (int)(Math.random() * 100);

int k = (int)(Math.random() * 3);

double f11 = f1;

double r1 = f11 / ( 10 ^ k);

}

class TesteMultiplicar{

publicstaticvoid main(String[] args){

Multiplicar m =new Multiplicar();

System.out.println(m.f11);

System.out.println("r1 = " + m.r1);

System.out.println(m.k);

System.out.println(m.f1 / (10 ^ m.k));

System.out.println(m.f2 / (10 ^ m.k));

}

}

as you can see, my idea was generating 3 digit ints and then divide them randomly by 1 or 10 or 100, but that doesnt work

can someone give me any ideas, pls?

thanks in advance

[1570 byte] By [newonthisa] at [2007-9-30 2:26:09]
# 1

What are you going to do with these numbers?

Fractional numbers (floating point numbers) are stored in binary and are approximations to decimal numbers. So requiring that the number itself be three decimal digits long is going to be hard and may involve big gaps in the produced numbers (where the binary representations do not coincide with decimal ones).

It seems to me that it would be easier to:

1) pick a random float and format it such that it displays 3 digits

or

2) pick a random int from 100 to 999 (assuming that you don't want leading zeroes) and then randomly stick a decimal point in one of the 4 positions.

paulcwa at 2007-7-16 13:36:06 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2

so far i have this:

class Multiplicar {

// gerar n齧eros

int f1 = (int)(Math.random() * 1000);

int f2 = (int)(Math.random() * 1000);

int k = (int)(Math.random() * 3);

double f11 = f1;

}

class TesteMultiplicar {

public static void main(String[] args) {

Multiplicar m = new Multiplicar();

double potencia = 1;

for (int i = 0; i < m.k; i++) {

potencia = potencia * 10;

}

System.out.println(m.k + " " + potencia);

System.out.println(m.f11 / potencia);

}

}

my prob now is that i dont know how to code exponents!..., so i had to loop...

any help would be welcome

newonthisa at 2007-7-16 13:36:06 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3

paulcw:

i posted again before i read your answer

i want these numbers to use them in an app i made to my children, so thatthey can exercise themselves in arithmetics - in this case multiplying to 3 digit numbers one by another

Do you thing i'm in the right path?

the only thing i need now is how to "code" 10 times 10 k times

newonthisa at 2007-7-16 13:36:06 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

my code now looks like this:

class Multiplicar {

// gerar n齧eros

int f1 = (int)(Math.random() * 1000);

int f2 = (int)(Math.random() * 1000);

int k1 = (int)(Math.random() * 3);

int k2 = (int)(Math.random() * 3);

double factor1 = f1;

double factor2 = f2;

}

class TesteMultiplicar {

public static void main(String[] args) {

Multiplicar m = new Multiplicar();

double potencia1 = 1;

for (int i = 0; i < m.k1; i++) {

potencia1 = potencia1 * 10;

}

double potencia2 = 1;

for (int i = 0; i < m.k2; i++) {

potencia2 = potencia2 * 10;

}

System.out.println(m.k1 + " " + potencia1);

System.out.println(m.k2 + " " + potencia2);

System.out.println(m.factor1 / potencia1);

System.out.println(m.factor2 / potencia2);

}

}

now only need some help in apropriate code that substitutes my loop

newonthisa at 2007-7-16 13:36:06 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5
Do you mean 3-digit whole numbers?If so why are you thinking about exponents at all?
paulcwa at 2007-7-16 13:36:06 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6
>i want to generate random numbers that allways have 3 digits:>123 or 1,23 or 12,3never mind; i got it - see my other threadBTW thanks
newonthisa at 2007-7-16 13:36:06 > top of Java-index,Archived Forums,New To Java Technology Archive...