Java Random Int getRandomInts(int number)

Here you can find the source of getRandomInts(int number)

Description

get Random Ints

License

Open Source License

Parameter

Parameter Description
number a parameter

Return

an array of integers of length number, where the array elements are randomly assigned a number between zero (incl.) and number (excl.), such that each element has a unique value.

Declaration

public static int[] getRandomInts(int number) 

Method Source Code


//package com.java2s;
/*/* ww w  .  jav a2 s  .  c om*/
 * RandomUtil.java
 * :tabSize=4:indentSize=4:noTabs=false:
 *
 * DingsBums?! A flexible flashcard application written in Java.
 * Copyright (C) 2006 Rick Gruber-Riemer (dingsbums@vanosten.net)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {
    private static Random rand = new Random();

    /**
     * @param number
     * @return an array of integers of length <code>number</code>, where the 
     *          array elements are randomly assigned a number between zero (incl.) and 
     *          <code>number</code> (excl.), such that each element has a unique value.  
     */
    public static int[] getRandomInts(int number) {
        int[] randInts = new int[number];
        List<Integer> temp = new ArrayList<Integer>(number);
        for (int i = 0; i < number; i++) {
            temp.add(i);
        }
        int pos;
        for (int i = 0; i < number; i++) {
            pos = rand.nextInt(temp.size());
            randInts[i] = temp.get(pos);
            temp.remove(pos);
        }
        return randInts;
    }
}

Related

  1. getRandomIntegerArray(Random rand, int size, int range)
  2. getRandomIntegerInRange(Random p_76136_0_, int p_76136_1_, int p_76136_2_)
  3. getRandomIntegerInRange(Random random, int i, int j)
  4. getRandomInterests()
  5. getRandomIntFromRange(int min, int max)
  6. getRandomIntStr(int len)
  7. rand(int min, int max)
  8. rand(int size)
  9. randBetween(int min, int max)