python:

1 import random
2
3 def randomMore(min,max,n):
4 res = []
5 while len(res) != n:
6 num = random.randrange(min,max)
7 if num not in res:
8 res.append(num)
9 print(res)
View Code
java:

1 import java.util.ArrayList;
2 import java.util.List;
3 import java.util.Random;
4 /**
5 * 1. 實作一個函數,生成給定範圍[min, max]内的n個随機正整數,入參為(min,max,n),且要求輸出的n個正整數不重複;
6 * 2. 為這個函數編寫一下測試用例,比如(1,100,10),(1,100,100)
7 */
8 public class randomDef {
9 //TODO
10 public List getRandom(int min, int max, int n){
11 List list = new ArrayList();
12 Random random = new Random();
13 while (list.size() != n){
14 int s = random.nextInt(max)%(max-min+1) + min;
15 if(!list.contains(s)){
16 list.add(s);
17 }
18 }
19 return list;
20 }
21
22 public static void main(String [] args){
23 randomDef list = new randomDef();
24 List l = list.getRandom(2,200,10);
25 System.out.print(l);
26 }
27 }