Here you can find the source of getRandomInts(int number)
Parameter | Description |
---|---|
number | a parameter |
number
, where the array elements are randomly assigned a number between zero (incl.) and number
(excl.), such that each element has a unique value.
public static int[] getRandomInts(int number)
//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; } }