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!!!

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)

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 ;)

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?

Member Avatar for iamthwee

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:

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.