当前位置:   article > 正文

java简单实现整数的随机重排_java 可重复的随机排列

java 可重复的随机排列

介绍

有时需要用到整数1-n的随机排列,如决定出场顺序等。这里用java做一个简单的小界面,显示整数1-n的随机排列。

这里界面用javafx工具实现。

原理

整数1-n的排列,共n!种,所以我们的目的相当于是从n!个样本点构成的总体中,随机等概率地抽取一个样本点。

当然,我们的实现算法不采取上述的n!的方式,需要采用计算量合理的算法。

一个可能出现的误区是:不断地产生1~n之间的随机整数;如果遇到与已经产生的整数出现重复,则舍弃掉这个整数,并继续产生,一直到产生n个不同的整数(>=1, <=n)。这个方法简单也直观,但从概率的角度看,这样产生的结果与上述的随机等概率抽样并不等价。

这里采用一种比较简单的方法:让计算机产生n个0~1均匀分布的随机数(一般我们使用的n较小,那么计算机产生的这n个随机并不重复),得到一个随机数序列。按顺序记录着n个随机数,即为每一个随机数贴上“标签”,标签上记录的是它在这个序列中的序号。

然后对这n个随机数排序。排序之后,随机数的位置自然发生了变换,我们按现在的位置顺序把它们的“标签”取出来,就得到一个1-n整数的随机排列。

实现

输入n后,建立一个数组和一个哈希表。n轮产生随机数,在第i轮中,将产生的这个随机数添加到数组,同时将其作为键添加到哈希表中,键对应的值为轮次,即整数i。

对数组排序。这里我用的是二路归并排序的方法。

按顺序取出排好序后的数组中的数,之后查找它在哈希表中对应的值。这样得到的值序列,就是我们想要的一个整数随机排列。

代码

  1. package random;
  2. import java.util.*;
  3. import javafx.application.Application;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.TextField;
  7. import javafx.scene.control.TextArea;
  8. import javafx.scene.layout.BorderPane;
  9. import javafx.scene.layout.HBox;
  10. import javafx.scene.text.Text;
  11. import javafx.stage.Stage;
  12. public class RandomPermutation extends Application {
  13. private Text text = new Text("Enter the maximun integer:");
  14. private TextField tfInput = new TextField();
  15. private TextArea ta = new TextArea();
  16. private Button btRun = new Button("Run");
  17. private Button btClear = new Button("Clear");
  18. private boolean allow = true;
  19. @Override
  20. public void start(Stage primaryStage) {
  21. // UI
  22. ta.setEditable(false);
  23. ta.setWrapText(true);
  24. BorderPane pane = new BorderPane();
  25. HBox hBox = new HBox();
  26. hBox.getChildren().addAll(text, tfInput, btRun, btClear);
  27. pane.setBottom(hBox);
  28. pane.setCenter(ta);
  29. Scene scene = new Scene(pane, 700, 400);
  30. primaryStage.setTitle("Random");
  31. primaryStage.setScene(scene);
  32. primaryStage.show();
  33. btRun.setOnAction(e -> runAction());
  34. btClear.setOnAction(e -> clearAction());
  35. }
  36. public static void main(String[] args) {
  37. Application.launch(args);
  38. }
  39. // Clear
  40. public void clearAction() {
  41. allow = true;
  42. ta.clear();
  43. tfInput.clear();
  44. }
  45. // Run
  46. public void runAction() {
  47. if(allow) {
  48. Integer n = Integer.parseInt(tfInput.getText());
  49. if(n instanceof Integer) {
  50. // Hash map to store key(random_number)-value(integer)
  51. Map<Double, Integer> hashMap = new HashMap<>();
  52. // Array to store random_number
  53. double[] randomNumbers = new double[n];
  54. for(int i = 1; i <= n; i++) {
  55. double r = Math.random();
  56. hashMap.put(r, i);
  57. randomNumbers[i - 1] = r;
  58. }
  59. // Sort the array
  60. mergeSort(randomNumbers);
  61. // Get the value(integer) sequence
  62. for(int i = 1; i <=n; i++) {
  63. double randomNumber = randomNumbers[i - 1];
  64. Integer integer = hashMap.get(randomNumber);
  65. ta.appendText(integer.toString() + " ");
  66. if( i % 15 == 0) {
  67. ta.appendText("\n");
  68. }
  69. }
  70. }
  71. allow = false;
  72. }
  73. }
  74. // Merge sort method
  75. public static void mergeSort(double[] list) {
  76. if(list.length > 1) {
  77. double[] firstHalf = new double[list.length / 2];
  78. System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
  79. mergeSort(firstHalf);
  80. int secondHalfLength = list.length - list.length / 2;
  81. double[] secondHalf = new double[secondHalfLength];
  82. System.arraycopy(list, list.length / 2, secondHalf, 0,
  83. secondHalfLength);
  84. mergeSort(secondHalf);
  85. merge(firstHalf, secondHalf, list);
  86. }
  87. }
  88. public static void merge(double[] list1, double[] list2, double[] temp) {
  89. int current1 = 0;
  90. int current2 = 0;
  91. int current3 = 0;
  92. while(current1 < list1.length && current2 < list2.length) {
  93. if(list1[current1] < list2[current2])
  94. temp[current3++] = list1[current1++];
  95. else
  96. temp[current3++] = list2[current2++];
  97. }
  98. while(current1 < list1.length)
  99. temp[current3++] = list1[current1++];
  100. while(current2 < list2.length)
  101. temp[current3++] = list2[current2++];
  102. }
  103. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/795268
推荐阅读
相关标签
  

闽ICP备14008679号