Java Doc for Random.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.util.Random

Random
public class Random implements java.io.Serializable(Code)
An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random . Java implementations must use all the algorithms shown here for the class Random , for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the method Math.random simpler to use.
author:
   Frank Yellin
version:
   1.54, 05/05/07
since:
   1.0



Field Summary
final static  longserialVersionUID
    

Constructor Summary
public  Random()
     Creates a new random number generator.
public  Random(long seed)
     Creates a new random number generator using a single long seed.

Method Summary
protected  intnext(int bits)
     Generates the next pseudorandom number.
public  booleannextBoolean()
    
public  voidnextBytes(byte[] bytes)
    
public  doublenextDouble()
    
public  floatnextFloat()
    
public synchronized  doublenextGaussian()
    
public  intnextInt()
    
public  intnextInt(int n)
    
public  longnextLong()
    
public synchronized  voidsetSeed(long seed)
     Sets the seed of this random number generator using a single long seed.

Field Detail
serialVersionUID
final static long serialVersionUID(Code)
use serialVersionUID from JDK 1.1 for interoperability




Constructor Detail
Random
public Random()(Code)
Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.



Random
public Random(long seed)(Code)
Creates a new random number generator using a single long seed. The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method Random.next .

The invocation new Random(seed) is equivalent to:

 
 Random rnd = new Random();  rnd.setSeed(seed);  

Parameters:
  seed - the initial seed
See Also:   Random.setSeed(long)




Method Detail
next
protected int next(int bits)(Code)
Generates the next pseudorandom number. Subclasses 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 by atomically updating the seed to

  (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)  
and returning
  (int)(seed >>> (48 - bits))  .
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 3: Seminumerical Algorithms, section 3.2.1.
Parameters:
  bits - random bits the next pseudorandom value from this random numbergenerator's sequence
since:
   1.1



nextBoolean
public boolean nextBoolean()(Code)



nextBytes
public void nextBytes(byte[] bytes)(Code)



nextDouble
public double nextDouble()(Code)



nextFloat
public float nextFloat()(Code)



nextGaussian
public synchronized double nextGaussian()(Code)



nextInt
public int nextInt()(Code)



nextInt
public int nextInt(int n)(Code)



nextLong
public long nextLong()(Code)



setSeed
public synchronized void setSeed(long seed)(Code)
Sets the seed of this random number generator using a single long seed. The general contract of setSeed is that it alters the state of this random number generator object so as to be in exactly the same state as if it had just been created with the argument seed as a seed. The method setSeed is implemented by class Random by atomically updating the seed to
  (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)  
and clearing the haveNextNextGaussian flag used by Random.nextGaussian .

The implementation of setSeed by class Random happens to use only 48 bits of the given seed. In general, however, an overriding method may use all 64 bits of the long argument as a seed value.
Parameters:
  seed - the initial seed




Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.