Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
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
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Paul Clapham
• Knute Snortum
• Rob Spoor
Saloon Keepers:
• Tim Moores
• Ron McLeod
• Piet Souris
• Stephan van Hulst
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

Ranch Hand
Posts: 35
Why i got an array of constantly same value when i use a object of class Random to produce a series of randomly-generated value. By doing what I can avoid it? How does the class Random work behind scenes and what is the so-called seed. I am longing for your detailed explanation, please make it easy to understand since i am a novice. THX!

Ranch Hand
Posts: 7729
Post your code and we'll see if we can help.

Ranch Hand
Posts: 1923
Since a random-generator generates it's values, they aren't as randomly as sometimes thought.

Modern systems use mouse- or keyboard-input, to generate numbers more randomly.

In most circumstances we don't need real random numbers, but something which looks like random.

Often there is a sequence of numbers generated, and this sequence is repeating itself, but after a very, very long time. A good algorithm will generate every possible value in the scope and with the same frequence.
The last generated number might be the start-value, to generate the next number, which leads to the same sequence for the same startvalue, the seed.

This looks like a drawback, but you may use a value based on the time to generate the seed, leading to new sequences for every run.
The same sequence might be very useful, when you compare two different implementations of an algorithm, which shall lead to the same result, but handle a lot of values, you don't like to generate manually.

Practical advice: Generate only one random = new Random () - object, and call random.next (...) on this common Object, which leads to a sequence of values.

Peng Fan
Ranch Hand
Posts: 35

everytime when i try to run this bit of code, i got a array of int of 20 same values, but if i uncomment the line 1 and line 3 and comment line 2, i can get what i want. THX!

 It is sorta covered in the JavaRanch Style Guide.