如何将1000随机平均分成四组?
1到1000如何随机平均分成四组? --------------------编程问答-------------------- 你想怎么平均 --------------------编程问答-------------------- 随机分成四组,每组250个! --------------------编程问答----------------------------------------编程问答-------------------- 数组序号除以4余数为0、1、2、3的各在一组 --------------------编程问答--------------------
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
list.add(i + 1);
}
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
List<Integer> list3 = new ArrayList<Integer>();
List<Integer> list4 = new ArrayList<Integer>();
Random r = new Random();
for (int i = 0; i < list.size(); i++) {
list1.add(list.remove(Math.abs(r.nextInt())%list.size()));
list2.add(list.remove(Math.abs(r.nextInt())%list.size()));
list3.add(list.remove(Math.abs(r.nextInt())%list.size()));
list4.add(list.remove(Math.abs(r.nextInt())%list.size()));
}
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
System.out.println(list4);
import java.util.Arrays;
import java.util.Random;
public class JustT {
/**
* @param args
*/
public static void main(String[] args) {
long start = System.currentTimeMillis();
int[] arr = new int[1000];
for (int i = 0; i < 1000; i++) {
arr[i] = i + 1;
}
System.out.println(Arrays.toString(arr));
int[] arr2 = new int[1000];
Arrays.fill(arr2, -1);
System.out.println(Arrays.toString(arr2));
for (int i = 0; i < 1000; i++) {
Random rd = new Random();
int id =-1;
while (true) {
id = rd.nextInt(1000);
if (arr2[i] == -1) {
if (arr[id] == -1) {
continue;
}
arr2[i] = arr[id];
arr[id] = -1;
// System.out.println(i + "=" + arr[id]);
break;
}
}
}
System.out.println(Arrays.toString(arr2));
for (int i = 0; i < 1000; i++) {
System.out.print(arr2[i] + ",");
if ((i + 1) % 250 == 0) {
System.out.println("\n");
}
}
long end = System.currentTimeMillis();
System.out.println("execute time:" + (end - start) + "ms");
}
}
好像复杂了。
楼上的那个方法好,哈哈 --------------------编程问答--------------------
int[] arr = new int[1000];--------------------编程问答--------------------
for (int i = 0; i < 1000; i++)
arr[i] = i;
List<Integer> arrayList = Arrays.asList(arr);
Collections.shuffle(arrayList);
List<Integer> group1 = arrayList.subList(0, 250);
List<Integer> group2 = arrayList.subList(250, 500);
List<Integer> group3 = arrayList.subList(500, 750);
List<Integer> group4 = arrayList.subList(750, 1000);
人家说的是随机
随机分配 不是固定的 3楼的是正确的 --------------------编程问答--------------------
看清楚了再拍砖啊 --------------------编程问答--------------------
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
list.add(i + 1);
}
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
List<Integer> list3 = new ArrayList<Integer>();
List<Integer> list4 = new ArrayList<Integer>();
Random r = new Random();
for (int i = 0; i < list.size(); i++) {
list1.add(list.remove(Math.abs(r.nextInt())%list.size()));
list2.add(list.remove(Math.abs(r.nextInt())%list.size()));
list3.add(list.remove(Math.abs(r.nextInt())%list.size()));
list4.add(list.remove(Math.abs(r.nextInt())%list.size()));
}
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
System.out.println(list4);
这样才对吧 --------------------编程问答--------------------
List<Integer> list = new ArrayList<Integer>();
for (; list.size()!=0;)
list.add(i + 1);
}
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
List<Integer> list3 = new ArrayList<Integer>();
List<Integer> list4 = new ArrayList<Integer>();
Random r = new Random();
for (int i = 0; i < list.size(); i++) {
list1.add(list.remove(Math.abs(r.nextInt())%list.size()));
list2.add(list.remove(Math.abs(r.nextInt())%list.size()));
list3.add(list.remove(Math.abs(r.nextInt())%list.size()));
list4.add(list.remove(Math.abs(r.nextInt())%list.size()));
}
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
System.out.println(list4);
--------------------编程问答-------------------- [code]int[] arr = new int[1000];
List<Integer> list = new ArrayList<Integer>();
for (; list.size()!=0;)
list.add(i + 1);
}
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
List<Integer> list3 = new ArrayList<Integer>();
List<Integer> list4 = new ArrayList<Integer>();
Random r = new Random();
for (; list.size()!=0; ) {
list1.add(list.remove(Math.abs(r.nextInt())%list.size()));
list2.add(list.remove(Math.abs(r.nextInt())%list.size()));
list3.add(list.remove(Math.abs(r.nextInt())%list.size()));
list4.add(list.remove(Math.abs(r.nextInt())%list.size()));
}
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
System.out.println(list4);
for (int i = 0; i < 1000; i++)
arr[i] = i;
List<Integer> arrayList = Arrays.asList(arr);
Collections.shuffle(arrayList);[/code]
为什么提示List<Integer> arrayList = Arrays.asList(arr);有错呢? --------------------编程问答-------------------- 提示
Type mismatch: cannot convert from List<int[]> to ArrayList<Integer>
--------------------编程问答-------------------- 拜你所赐,让我好一阵研究多线程
我的思路是这样的:
1.1000个数字弄4个线程,每个线程绑定一个List。每一个数字都让线程去抢,抢到放入自己的list当中;
2.当其中一个线程抢够250个数字,就退出本次争抢,四个线程都够250就把1000分完了
不过后来想了想对于解决你的问题其实不太合适,所以就补贴代码了,呵呵
补充:Java , Java SE