Java non-repetitive random number generation
Import java.util.*;
public class test{
public static void Main (string[] args) {
Generate [0-n] a random number that is not repeated
/ST To save these random numbers
ArrayList list = new ArrayList ();
int n = 10;
Random rand = new Random ();
boolean[] bool = new Boolean[n];
int num = 0;
for (int i = 0; i<n; i++) {
do{
If the resulting number is the same as continuing the loop
num = Rand.nextint (n);
}while (Bool[num]);
Bool[num] =true;
List.add (num);
}
SYSTEM.OUT.PRINTLN (list);
}
public class Test
{
public static void Main (string[] args)
{
int[] arr = new INT[10];
for (int i = 0; i < i++)
{
Arr[i] = (int) (Math.random () * 40) + 1;
for (int j = 0; J < i; J + +)
{
if (arr[j] = = Arr[i])
{
i--;
Break
}
}
}
for (int i = 0; i < i++)
System.out.print (Arr[i] + "");
}
}
B.
Java Code
Import java.util.*;
public class Test
{
public static void Main (string[] args)
{
int n=40;
int[] num = new Int[n];
for (int i=0;i <num.length;i++)
Num[i] = i+1;
int[] arr = new INT[10];
for (int i=0;i <arr.length;i++)
{
int r = (int) (Math.random () *n);
ARR[I]=NUM[R];
NUM[R]=NUM[N-1];
n--;
}
for (int i=0;i <arr.length;i++)
System.out.print (arr[i]+ "");
}
}
C.
Java Code
Import java.util.*;
public class Test
{
public static void Main (string[] args)
{
LinkedList <Integer> mylist= new LinkedList <Integer> ();
int n=40;
for (int i=0;i <n;i++)
Mylist.add (i+1);
int[] arr = new INT[10];
for (int i=0;i <arr.length;i++)
{
Arr[i]=mylist.remove ((int) (Math.random () *n));
n--;
}
for (int i=0;i <arr.length;i++)
{
System.out.print (arr[i]+ "");
}
}
}
D.
Java Code
Import java.util.*;
public class Test
{
public static void Main (string[] args)
{
Set <Integer> myset = new Linkedhashset <Integer> ();
while (Myset.size () <10)
{
Myset.add ((int) (Math.random () *40+1));
}
for (Integer I:myset)
{
System.out.print (i+ "");
}
}
}
The first method, which randomly produces a number in an array to be selected, then puts him at the end of the array to be selected, then randomly generates the next random number from the length-1, and so on.
public static int[] Randoms ()
{
Random r = new Random ();
int temp1,temp2;
int send[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
int len = send.length;
int returnvalue[] = new INT[22];
for (int i=0;i<22;i++)
{
Temp1 = Math.Abs (R.nextint ())% Len;
Returnvalue[i] = Send[temp1];
Temp2 = Send[temp1];
SEND[TEMP1] = send[len-1];
Send[len-1] = Temp2;
len--;
}
Return returnvalue;
}
}
Method Two: Or a fixed no repetition of the array, and then the array of random swap position, after many times the array is a random array without duplicates.
public static int[] Random2 ()
{
int send[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
int Temp1,temp2,temp3;
Random r = new Random ();
for (int i=0;i<send.length;i++)///random exchange send.length times
{
Temp1 = Math.Abs (R.nextint ())% (send.length-1); Randomly generate a position
Temp2 = Math.Abs (R.nextint ())% (send.length-1); Randomly generating another location
if (Temp1!= temp2)
{
Temp3 = Send[temp1];
SEND[TEMP1] = SEND[TEMP2];
SEND[TEMP2] = Temp3;
}
}
return send;
}
Java generates random passwords of specified lengths
Finished, the results are not used, recorded here, to prevent loss.
Import Java.util.Random;
public class Test {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
System.out.println (Genrandomnum (10));
}
/**
* Generate the password immediately
* @param the total length of the password generated by Pwd_len
* @return A string of passwords
*/
public static String genrandomnum (int pwd_len) {
35 is because the array is starting from 0, 26 letters + 10 digits
final int maxnum = 36;
int i; Generated random number
int count = 0; The length of the generated password
char[] str = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ',
' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' W ',
' x ', ' y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
StringBuffer pwd = new StringBuffer ("");
Random r = new Random ();
while (Count < Pwd_len) {
Generates random numbers, takes absolute values, and prevents negative numbers from being generated,
i = Math.Abs (R.nextint (maxnum)); The maximum number of generated is 36-1
if (i >= 0 && i < str.length) {
Pwd.append (Str[i]);
Count + +;
}
}
return pwd.tostring ();
}
}