0

I'm trying to make a program where Math.random() generated a list of random numbers as ints between the range of 3 and 9. I keep getting long decimals and numbers such as 0, or 1. I'm not sure if it's the output() or is my process right? Or is it something simple like typecasting?

Here's the output i'm getting with this code:

    --------------------Configuration: <Default>--------------------

    5.855672310088313
    5.0287286763569625
    5.1257975957882485
    3.2283987551436173
    0.8364642553235297
    3.5005965809506696
    3.284959203422762
    1.3896135045374878
    2.8493705949526733
    0.7431149270852646

Process completed.

Im trying to get something more along the lines of a list of 10 random numbers between 3 and 9.

import static java.lang.System.*;
import java.util.*;

public class Java1605{
    public static void main(String[] args){
        new Problem();
}}


class Problem
{
    int low;
    int high;
    int range;

    public Problem()
    {
        low = 3;
        high = 9;
        range = (int)((Math.random() * (high - low )) + low);

        output();
    }

    public void output()
    {
        out.println(Math.random()* range);
        out.println(Math.random()* range);
        out.println(Math.random()* range);
        out.println(Math.random()* range);
        out.println(Math.random()* range);
        out.println(Math.random()* range);
        out.println(Math.random()* range);
        out.println(Math.random()* range);
        out.println(Math.random()* range);
        out.println(Math.random()* range);


    }

}
duffymo
  • 293,097
  • 41
  • 348
  • 541
Dommy Bomb
  • 15
  • 5
  • 1
    Why are you calling "Math.random () * range" inside your ouput method? Just print the result, as your "range" variable should already be inside your range. – Ansharja Oct 22 '17 at 23:13
  • That 'range' is not something you multiply with, that is your random number between 'low' and 'high'. Apply some common sense: how would you "move" a number like 0 between two other numbers, using multiplication only? – tevemadar Oct 22 '17 at 23:35

0 Answers0