Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Random() combined with a loop = trouble?  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some problems to make a lot of random numbers. Below
you will find my program code. The random generated number
is the same as often as the loop is running. What am I doing wrong???
Can anybody help me?
Thanx
Bonsai

import java.util.Random;
public class NumberGenerator
{
public NumberGenerator()
{
}
public int generateNumber()
{
int s = 6; //for example for a six-sided dice
int result = (int) ((new Random().nextFloat()) * s)+1;

/** As alternative to the previous line, but also not
* working.
*
* int temp = (int) Math.round(Math.random() * s)+1;
*/
return result;
}
}
//---------------------------------------------------------------
import java.io.*;
import NumberGenerator;
public class mainProg
{
public static void main(String[] args)
{
NumberGenerator ng = new NumberGenerator();

/* This loop should create 15 different(!) pseudo-random numbers.
* Unfortunately I get the same numbers every time.
*/
for(int i = 0; i < 15; i++)
{
int consoleOutput = ng.generateNumber();
System.out.println(consoleOutput);
}

}
}
 
Ranch Hand
Posts: 64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai, Bonsai,
I just tried that using Math.random() and it works like a charm. I got the same problem you did with new Random().nextFloat() however I noticed that the number does change infrequently during the execution of the program...which means that perhaps after a set amount of time a new random number is produced.
If you place a Thread.sleep(1000) command inside your loop (with a try-catch, as well) or inside your method that is called within the loop, this will cause the "random" numbers to be different each time...perhaps the Random objects are being created too quickly and thus each time they are created they are using the same first number.
In order to get to the root of this matter, I think one would have to be aware of the inner workings of the Random class.
I hope that helps somewhat, anyway.
Paul

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use the random class it is definitely a good idea to use a pause like Paul said. If I am correct I believe the the random class generates random numbers based on the current time. So if your next random number is generated very quickly it will be very close to the original.
 
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

In the 18 years since the original post, things have changed, and java.util.Random no longer uses time like that.
 
Saloon Keeper
Posts: 9710
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The right tool for the right job: While it's never a good idea to use Random for security of sensitive data, in general it's also not a good idea to use SecureRandom for normal pseudo-random number generation. The reason is that SecureRandom is very slow compared to Random, and you may actually not even want the values to be as random as possible. When you want to repeat a sequence of random numbers (for instance, when you've reloaded a turn-based game and you want randomly occurring events to unfold the same way they did before) you use a Random instance that was instantiated with a specified seed.

When you have a class that needs to generate values, don't let it generate the values itself. Instead, pass a factory in a constructor. That way, you can unit test methods that rely on random values, by passing a subclass of Random that returns a fixed sequence.
 
The City calls upon her steadfast protectors. Now for a tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!