• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How Random class works??

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How Random class shuffles the numbers?
I know it works on linear congruential formula.
Can any one explain it with an example
Thanks
 
Rancher
Posts: 43009
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're really curious, why not look at the source? It comes with the JDK in a file called src.zip, and the Random class is written completely in Java, and heavily documented.
[ June 13, 2006: Message edited by: Ulf Dittmer ]
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you bothered to look in the API for Random? The protected next method is key:

protected int next(int bits)

Generates the next pseudorandom number. Subclass should override this, as this is used by all other methods.

The general contract of next is that it returns an int value and if the argument bits is between 1 and 32 (inclusive), then that many low-order bits of the returned value will be (approximately) independently chosen bit values, each of which is (approximately) equally likely to be 0 or 1. The method next is implemented by class Random as follows:

This is a linear congruential pseudorandom number generator, as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.2.1.
 
naidu rasagna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for quick reply.
But how it works on a collection??
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by naidu rasagna:
Thanks for quick reply.
But how it works on a collection??



???

What do you mean?
 
naidu rasagna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In coleections class there is a method called shuffle and it takes Random object as one parameter.So how will it be useful to shuffle the objects in that collection?
Thanks
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by naidu rasagna:
In coleections class there is a method called shuffle and it takes Random object as one parameter.So how will it be useful to shuffle the objects in that collection?
Thanks



Thanks for writing what method you are asking about. Have you read the javadoc for the shuffle method? Have you unzipped and looked at the implementation, as another poster has already suggested?
 
naidu rasagna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,I unzipped and see the code for the random class but i am confused what is 48 bit seed and how it works for collection elements .
Thanks
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your original post it was unclear that you were referring to the shuffle method of class java.util.Collections.

Have you studied the javadoc comments for shuffle in the API?

Have you looked at the source code for method shuffle?
 
naidu rasagna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes but i didnt get nextInt used in swap method in shuffle method.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by naidu rasagna:
yes but i didnt get nextInt used in swap method in shuffle method.



What don't you get: the defintion of nextInt in Random or the use of nextInt in method shuffle?
 
naidu rasagna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The nextInt method used in shuffle method calls the Random classes nextInt method and it calls next method of random class.I didnt understand the nextInt and next methods of Random class...
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method nextInt is very easy to understand:


But in the end, this entire thread comes down to asking what you really want to know and why:

-- what shuffle does, its specification
-- how shuffle is implemented, its code
-- what nextInt does, its specification
-- how nextInt is implemented, its code
-- what next does, it's specification
-- how next is implemented, its code

The trickiest technical part is how linear conguence algorithms general good pseudo random number sequences, and how that is implemented in Java in method next. It's also the least important thing to know. The most important stuff is in the specications, not the implementations.

So after all this, if you still insist that need to know how linear conguence works, you'll need to read Knuth. It's no accident his name rhymes with truth.
 
naidu rasagna
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!