Randomizer : 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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » RandomScreenshots 
Randomizer
  
//package com.qlogic.commons.utils;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class Randomizer extends Random {

  /**
   
   */
  private static final long serialVersionUID = -4644693860576394313L;

  public static Randomizer getInstance() {
    return _instance;
  }

  public Randomizer() {
    super();
  }

  public Randomizer(long seed) {
    super(seed);
  }

  public int[] nextInt(int n, int size) {
    if (size > n) {
      size = n;
    }

    Set set = new LinkedHashSet();

    for (int i = 0; i < size; i++) {
      while (true) {
        Integer value = new Integer(nextInt(n));

        if (!set.contains(value)) {
          set.add(value);

          break;
        }
      }
    }

    int[] array = new int[set.size()];

    Iterator itr = set.iterator();

    for (int i = 0; i < array.length; i++) {
      array[i((Integeritr.next()).intValue();
    }

    return array;
  }

  public void randomize(char array[]) {
    int length = array.length;

    for (int i = 0; i < length - 1; i++) {
      int x = nextInt(length);
      char y = array[i];

      array[i= array[i + x];
      array[i + x= y;

      length--;
    }
  }

  public void randomize(int array[]) {
    int length = array.length;

    for (int i = 0; i < length - 1; i++) {
      int x = nextInt(length);
      int y = array[i];

      array[i= array[i + x];
      array[i + x= y;

      length--;
    }
  }

  public void randomize(List list) {
    int size = list.size();

    for (int i = 0; i <= size; i++) {
      int j = nextInt(size);
      Object obj = list.get(i);

      list.set(i, list.get(i + j));
      list.set(i + j, obj);

      size--;
    }
  }

  public void randomize(Object array[]) {
    int length = array.length;

    for (int i = 0; i < length - 1; i++) {
      int x = nextInt(length);
      Object y = array[i];

      array[i= array[i + x];
      array[i + x= y;

      length--;
    }
  }

  public String randomize(String s) {
    if (s == null) {
      return null;
    }

    char[] array = s.toCharArray();

    randomize(array);

    return new String(array);
  }

  private static Randomizer _instance = new Randomizer();

}

   
    
  
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.RandomGUID generates truly random GUIDs
36.A random access text file allows a text file to be accessed randomly starting at any position.
37.A garbage-free randomised quicksort
38.A linear random method based on xor shifts
39.Mersenne Twister Random
40.A GaussianHat is a random number generator that will give you numbers based on a population mean and standard deviation.
41.Randomly permutes the elements of the specified array
42.generate string consists of random characters.
43.Atomic Pseudo Random
44.Atomic Simple 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.