Atomic Simple Random : Random « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Random 




Atomic Simple Random
  
/*
 * @(#)$Id: codetemplate_xbird.xml 943 2006-09-13 07:03:37Z yui $
 *
 * Copyright (c) 2005-2006 Makoto YUI and Project XBird
 * All rights reserved.
 
 * This file is part of XBird and is distributed under the terms of
 * the Common Public License v1.0.
 
 * Contributors:
 *     Makoto YUI - initial implementation
 */
//package xbird.util.concurrent.lang;

import java.util.concurrent.atomic.AtomicLong;

/**
 
 * <DIV lang="en"></DIV>
 * <DIV lang="ja"></DIV>
 
 @author Makoto YUI ([email protected])
 */
public final class AtomicSimpleRandom {

    private final static long multiplier = 0x5DEECE66DL;
    private final static long addend = 0xBL;
    private final static long mask = (1L << 481;

    private static final AtomicLong seq = new AtomicLong(-715159705);
    private long seed;

    public AtomicSimpleRandom(long s) {
        this.seed = s;
    }

    public AtomicSimpleRandom() {
        this.seed = System.nanoTime() + seq.getAndAdd(129);
    }

    public void setSeed(long s) {
        seed = s;
    }

    public int nextInt() {
        return next();
    }

    public int next() {
        long nextseed = (seed * multiplier + addend& mask;
        this.seed = nextseed;
        return ((int) (nextseed >>> 17)) 0x7FFFFFFF;
    }

    public int next(int bits) {
        long nextseed = (seed * multiplier + addend& mask;
        this.seed = nextseed;
        return (int) (nextseed >>> (48 - bits));
    }

}

   
    
  














Related examples in the same category
1.Create random number
2.Create two random number generators with the same seed
3.Does Math.random() produce 0.0 and 1.0
4.Random numbers between 1 and 100
5.Random number between 0 AND 10
6.Random integers that range from from 0 to n
7.Random.nextInt(n) returns a distributed int value between 0 (inclusive) and n (exclusive).
8.Round Java float and double numbers using Math.round
9.Randomizer
10.nextDouble() and nextGaussian() in java.util.Random
11.Generating random numbers
12.Generate random ints by asking Random() for
13.Getting random numbers: nextGaussian
14.Generate a random array of numbers
15.Next double and next int
16.Random bytes
17.Random boolean
18.Random long type number
19.Random float type number
20.Random double type number
21.Math.random
22.A wrapper that supports all possible Random methods via the java.lang.Math#random() method and its system-wide {@link Random} object.
23.Operations for random Strings
24.Random Util with ReentrantLock
25.A Java implementation of the MT19937 (Mersenne Twister) pseudo random number generator algorithm
26.Randomizer
27.Random: Properly synchronized and can be used in a multithreaded environment
28.A predictable random number generator.
29.Random GUID
30.A random.org seeded random generator.
31.Generates a secure random word with the given length consisting of uppercase and lowercase letters and numbers.
32.A random choice maker
33.Memory-efficient map of keys to values with list-style random-access semantics.
34.Generates a random integer inside the lo and hi interval
35.Randomizer
36.RandomGUID generates truly random GUIDs
37.A random access text file allows a text file to be accessed randomly starting at any position.
38.A garbage-free randomised quicksort
39.A linear random method based on xor shifts
40.Mersenne Twister Random
41.A GaussianHat is a random number generator that will give you numbers based on a population mean and standard deviation.
42.Randomly permutes the elements of the specified array
43.generate string consists of random characters.
44.Atomic Pseudo Random
45.Mersenne
46.Mersenne Twister
47.Mersenne Twister Fast
48.Mersenne Twister algorithm
49.Emulates a dice
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.