0
public class Generator {
    static Random rnd= new Random();

    private static void generator() {

        double zahl = 0;
        double min = 0.0;// minimum number
        double max = 1.0;// maximal number
        int x = 0; // number of numbers you want to output

        System.out.print("How many numbers:  ");
        x = Eingabe.liesInt(); //User-input of the numbers
        rnd.setSeed(123423);

        for (int i = 0; i < x; i++) {
            zahl = (int) ((rnd.nextDouble() * ((max - min) + 1) + min));
            System.out.println(zahl);

        }
        System.out.println();
    }
    /*
     * Standard-Constructor
     */
    public static void main(String[] args) {
        generator();
    }
}

Can somebody help me? What is wrong with tis code? I want to get numbers between 0.0 and 1.0! The only numbers I get are really 0 and 1, but I also want numbers between!!!

Edited by mike_2000_17: Fixed formatting

4
Contributors
6
Replies
7
Views
11 Years
Discussion Span
Last Post by spacedani
0

You're casting the double to an integer. Integers don't have decimals, so the fractional part is being cut off or rounded to the nearest whole. (i forget which)

0

It's truncated. The decimal portion is cut off.

1.8 == 1
1.1 == 1
1.99999 == 1

At least that's what I remember ;)

0

Thank you! I´ve changed the code.

Now I have to save the generated numbers in a textfile, something like "number.txt". How can I do that?

0

Thank you! I´ve changed the code.

Now I have to save the generated numbers in a textfile, something like "number.txt". How can I do that?

import java.io.*;

public class WriteFile
{
    public static void main(String args[])
    {
        try
        {
            FileWriter file = new FileWriter("C:/Iamthwee.txt");
            //this is where ur text file will be
            BufferedWriter buffer = new BufferedWriter(file);
            
            for (int i=0; i<100; i++)
            {
            buffer.write("random numbers here");
            buffer.newLine();
            }
            buffer.close();
            
        }
        catch (IOException e) {System.out.println(e);}
    }
}

Add 2 ur random number method and that's it. :cool:

0

buffer.write("random numbers here");
buffer.newLine();
}

this only works with INT but not with DOUBLE!!! What must I change?

0

ok I have it!! I only converted the double to string! Now it works. Thank you for your help!!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.