mardi 29 mars 2016

Random decimal between 0 and 1 stored in an int, Java

Working on a project, trying to find a way to generate a random decimal between 0 and 1, inclusive. Making a multi-threaded project that will increase and decrease an (int) number.

So far I have declared a private int number, initialized to 0 and also a random generator.

The way I am hoping to get this to work is to get a random decimal between 0 and 1 (I.E. 0.564184) and add it to number.

The way the program is designed is to check if number is less than 1, if it is then add a random decimal to it, re run the check until it is above 1 or the next number will make it above 1

I saw an example on Java random number between 0 and 0.06 But when i attached it in and modified it to this :

 while(number < max)
          {         
            double increase = random.nextInt(2);
            increase = increase / 10.0;
            number = (int) (number + increase);
            System.out.println(number);
          }

The program continuously runs just printing out 0's. I do believe this is due to the cast to int when adding the number before printing.

I have the threads working when I just use
number = number - random.nextInt(1) + 1; System.out.println(number);

Now I am stuck, looking different guides, I thought while I am looking I should ask for assistance. Thanks ahead of time.




Aucun commentaire:

Enregistrer un commentaire