public class testCSDN { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; int loc = 0; for (int i = 0; i < 7; i ++){ loc = (int)(Math.random()*19); int temp = a[i]; a[i] = a[loc]; a[loc] = temp; } for (int i = 0; i < 7; i ++){ System.out.print(a[i] + " "); } } }
public class testCSDN { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; int loc = 0; for (int i = 0; i < 7; i ++){ loc = (int)(Math.random()*19); int temp = a[i]; a[i] = a[loc]; a[loc] = temp; } for (int i = 0; i < 7; i ++){ System.out.print(a[i] + " "); } } } 楼主可以试试
import java.util.*;public class TestRandom { public static void main(String[] args) { Set<Integer> integers = new HashSet<Integer>(); Random random = new Random(47); while (integers.size() < 7) integers.add(random.nextInt(20) + 1); System.out.println(integers); } }
在List里面放1-20吧 然后用int i = list.remove(int index); 这样应该能保证不重复吧
Random,放到set集合中,再判断下就OK了
用TreeSet import java.util.Collection; import java.util.Random; import java.util.TreeSet;public class DistinctNum { public static Collection<Integer> getDistinctNum(int count) { //count产生的数据个数 Collection<Integer> set = new TreeSet<Integer>(); Random rand = new Random(); while(set.size()<count){ set.add(rand.nextInt(100)); } return set; }} 试试
参考一下:import java.util.ArrayList; import java.util.List; import java.util.Random;public class RandomTest { public static void main(String[] args) { Random random = new Random(); List<Integer> list = new ArrayList<Integer>(); int num = random.nextInt(20) + 1; while (list.size() < 7) { if (list.contains(num)) continue; list.add(num); num = random.nextInt(20) + 1; } for (int i : list) System.out.print(i + " "); } }
也可利用种子: Random(long seed) Creates a new random number generator using a single long seed.由于不同的种子,随机数序列不同,例用1作种子: Random ran = new Random(1); 用当前的系统时间作为种子 Random ran= new Random((new Date()).getTime());只要种子不同,随机数不会同,这样少了一次判断。
看来我错了,要保证一定不重复,要有相应措施,如用HashSet。 import java.util.*; public class Test{ public static void main(String args[]){ Set<Integer> rand = new HashSet<Integer>(); Random random = new Random(); do rand.add(random.nextInt(20) + 1); while (rand.size() < 7);
System.out.println(rand); } }
用List的shuffle也行: import java.util.*; public class Test{ public static void main(String[] args){ List<Integer> list = new ArrayList<Integer>(); for(int i=1; i<20; i++) { list.add(i); } Collections.shuffle(list); for(int i=0; i<7; i++) System.out.print(list.get(i)+" "); } }
如果使用List就直接使用Collections.shuffle就行了,然后取前N个。这样效率才高如果是数组也是一样参考里面的实现:int N = 20; int[] list = new int[N]; for (int i = 1; i <= N; i++) list[i - 1] = i; Random rnd = new Random(); for (int i = N; i > 1; i--) { int j = rnd.nextInt(i); int tmp = list[j]; list[j] = list[i - 1]; list[i - 1] = tmp; } System.out.println(Arrays.toString(list));
import java.util.*;public class TestRandom { public static void main(String[] args) { HashSet<Integer> hs = new HashSet<Integer>(); while(hs.size() < 7) { hs.add((int)(Math.random() * 20)); } for(Iterator it = hs.iterator(); it.hasNext();) { System.out.println(it.next()); } } } 跟10楼差不多
续上,根据该方法写的代码:package net.csdn;import java.util.Random;public class RandomSerial {
public static final Random RANDOM_CREATOR = new Random( System.currentTimeMillis() );
/** * Create an random integer serial, in which all element is different from each other. * @param range Range of the serial, that means every element of the serial is between 1 and serial * @param length Length of the seiral * @return The array format of the created serial * @author xiaoqiao_82 */ public static int[] getSerial( int range, int length ) {
if( range<1 || length>range ) { throw new IllegalArgumentException( "Cannot create serial of range " + range + " and length " + length ); } int[] source = new int[range]; for( int i=0; i<source.length; i++ ) { source[i] = i; } int [] serial = new int[length]; int num; int max;
for( int i=0; i<length; i++ ) { max = range - i; num = RANDOM_CREATOR.nextInt( max ); serial[i] = source[num] + 1; source[num] = source[max-1]; }
return serial; }
public static void printSerial( int[] serial ) { if( null==serial || 0==serial.length ) { System.out.println( "The serial is empty!" ); } else { System.out.print( "The serial: " ); for( int i=0; i<serial.length-1; i++ ) { System.out.print( serial[i] + ", " ); } System.out.println( serial[serial.length-1] ); } }
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int loc = 0;
for (int i = 0; i < 7; i ++){
loc = (int)(Math.random()*19);
int temp = a[i];
a[i] = a[loc];
a[loc] = temp;
}
for (int i = 0; i < 7; i ++){
System.out.print(a[i] + " ");
}
}
}
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int loc = 0;
for (int i = 0; i < 7; i ++){
loc = (int)(Math.random()*19);
int temp = a[i];
a[i] = a[loc];
a[loc] = temp;
}
for (int i = 0; i < 7; i ++){
System.out.print(a[i] + " ");
}
}
}
楼主可以试试
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Num {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
//初始化list
for(int i=1; i<20; i++) {
list.add(i);
}
int[] array = new int[7];
Random random = new Random();
for(int i=0; i<array.length; i++) {
array[i] = list.remove(random.nextInt(list.size()));
}
System.out.println("产生的不重复的随机数:");
System.out.println(Arrays.toString(array));
}
}
import java.util.*;public class TestRandom {
public static void main(String[] args) {
Set<Integer> integers = new HashSet<Integer>();
Random random = new Random(47);
while (integers.size() < 7)
integers.add(random.nextInt(20) + 1);
System.out.println(integers); }
}
这样应该能保证不重复吧
import java.util.Collection;
import java.util.Random;
import java.util.TreeSet;public class DistinctNum {
public static Collection<Integer> getDistinctNum(int count) { //count产生的数据个数
Collection<Integer> set = new TreeSet<Integer>();
Random rand = new Random();
while(set.size()<count){
set.add(rand.nextInt(100));
}
return set; }}
试试
import java.util.List;
import java.util.Random;public class RandomTest {
public static void main(String[] args) {
Random random = new Random();
List<Integer> list = new ArrayList<Integer>();
int num = random.nextInt(20) + 1;
while (list.size() < 7) {
if (list.contains(num))
continue;
list.add(num);
num = random.nextInt(20) + 1;
}
for (int i : list)
System.out.print(i + " "); }
}
Random(long seed)
Creates a new random number generator using a single long seed.由于不同的种子,随机数序列不同,例用1作种子:
Random ran = new Random(1);
用当前的系统时间作为种子
Random ran= new Random((new Date()).getTime());只要种子不同,随机数不会同,这样少了一次判断。
import java.util.*;
public class Test{
public static void main(String args[]){
Set<Integer> rand = new HashSet<Integer>();
Random random = new Random(); do rand.add(random.nextInt(20) + 1);
while (rand.size() < 7);
System.out.println(rand);
}
}
import java.util.*;
public class Test{
public static void main(String[] args){
List<Integer> list = new ArrayList<Integer>();
for(int i=1; i<20; i++) {
list.add(i);
}
Collections.shuffle(list);
for(int i=0; i<7; i++)
System.out.print(list.get(i)+" ");
}
}
方式2可以保证运算次数固定,不存在运气好与不好的问题
#include<iostream>
#include<cstdlib>
using namespace std;
void main()
{
int s[7]={0};
int x = rand()%20;
int n=0;
for(int i=0;n<7;i=0)
{
while(i< n&&x != s[i])
i++;
if(i==n)
{
s[n]=x;
++n;
x= rand()%20;
}
}
for(int j=0;j<7;j++)
{
cout<<s[j]<<"\t";
}
cout<<endl;
}
第二次,在0到18中roll随机数,把它当作数组下标从数组中取数,然后把数组中第19个元素换位置。
.....
不但不重复,而且只roll7次,只是加了两个自减,一个交换就OK了。
开销不是更大 又不是linklist
int[] list = new int[N];
for (int i = 1; i <= N; i++)
list[i - 1] = i;
Random rnd = new Random();
for (int i = N; i > 1; i--) {
int j = rnd.nextInt(i);
int tmp = list[j];
list[j] = list[i - 1];
list[i - 1] = tmp;
}
System.out.println(Arrays.toString(list));
public static void main(String[] args) {
HashSet<Integer> hs = new HashSet<Integer>();
while(hs.size() < 7) {
hs.add((int)(Math.random() * 20));
}
for(Iterator it = hs.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}
}
跟10楼差不多
public static final Random RANDOM_CREATOR = new Random( System.currentTimeMillis() );
/**
* Create an random integer serial, in which all element is different from each other.
* @param range Range of the serial, that means every element of the serial is between 1 and serial
* @param length Length of the seiral
* @return The array format of the created serial
* @author xiaoqiao_82
*/
public static int[] getSerial( int range, int length ) {
if( range<1 || length>range ) {
throw new IllegalArgumentException( "Cannot create serial of range " + range +
" and length " + length );
} int[] source = new int[range];
for( int i=0; i<source.length; i++ ) {
source[i] = i;
}
int [] serial = new int[length];
int num;
int max;
for( int i=0; i<length; i++ ) {
max = range - i;
num = RANDOM_CREATOR.nextInt( max );
serial[i] = source[num] + 1;
source[num] = source[max-1];
}
return serial;
}
public static void printSerial( int[] serial ) {
if( null==serial || 0==serial.length ) {
System.out.println( "The serial is empty!" );
} else {
System.out.print( "The serial: " );
for( int i=0; i<serial.length-1; i++ ) {
System.out.print( serial[i] + ", " );
}
System.out.println( serial[serial.length-1] );
}
}
public static void main(String [] args) {
for( int i=0; i<10; i++ ) {
printSerial( getSerial(20, 7) );
}
System.out.println( "--------------------------------" );
for( int i=0; i<10; i++ ) {
printSerial( getSerial(4, 4) );
}
}}
运行结果:
The serial: 19, 10, 1, 17, 20, 8, 4
The serial: 16, 3, 13, 2, 19, 1, 6
The serial: 4, 8, 15, 7, 1, 11, 20
The serial: 14, 4, 18, 17, 15, 13, 11
The serial: 2, 3, 11, 12, 19, 9, 20
The serial: 12, 17, 3, 7, 20, 16, 19
The serial: 10, 8, 7, 5, 11, 15, 6
The serial: 5, 2, 4, 17, 8, 3, 20
The serial: 14, 10, 9, 19, 17, 20, 7
The serial: 15, 14, 11, 7, 3, 16, 9
--------------------------------
The serial: 1, 4, 3, 2
The serial: 4, 1, 3, 2
The serial: 4, 1, 3, 2
The serial: 1, 4, 2, 3
The serial: 3, 1, 4, 2
The serial: 4, 3, 2, 1
The serial: 2, 3, 4, 1
The serial: 4, 2, 3, 1
The serial: 3, 1, 4, 2
The serial: 4, 1, 3, 2