SHARE
TWEET

Untitled

a guest Apr 21st, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. public class Main {
  5.     static float _a = (float) 0.66;
  6.     static int max_arr = 10000;
  7.  
  8.     public static void main(String[] args) {
  9.         Random rnd = new Random();
  10.         float x = rnd.nextFloat();
  11.         float y = rnd.nextFloat();
  12.         for (int i = 0; i < max_arr; i++) {
  13.             method_reject(x, y);
  14.         }
  15.         repeat(10);
  16.         no_repeat(10);
  17.     }
  18.  
  19.     static float _y(float x) {
  20.         float y;
  21.         if (x >= 0.0 && x <= 1.0) {
  22.             y = _a * x;
  23.         } else if (x > 1.0 && x < 2.0) {
  24.             y = _a;
  25.         } else {
  26.             y = 0;
  27.         }
  28.         return y;
  29.     }
  30.  
  31.  
  32.     static void method_reject(float x1, float x2) {
  33.         float a = 0, b = 2;
  34.         float c = (float) 0.66;
  35.         Random rnd = new Random();
  36.         do {
  37.             x1 = rnd.nextFloat();
  38.             x2 = rnd.nextFloat();
  39.         } while (_y(a + (b - a) * x1) < c * x2);
  40.         x2 *= c;
  41.         x1 = a + (b - a) * x1;
  42.     }
  43.  
  44.  
  45.     static void repeat(int generated_numbers) {
  46.         float residue_chance = 1, chance;
  47.         Random rnd = new Random();
  48.         float chance_hit[] = new float[generated_numbers];
  49.         int hit[] = new int[generated_numbers];
  50.  
  51.         for(int number = 0; number < generated_numbers - 1; number++) {
  52.             chance_hit[number] = rnd.nextFloat();
  53.             residue_chance -= chance_hit[number];
  54.         }
  55.         chance_hit[generated_numbers - 1] = residue_chance;
  56.  
  57.  
  58.         for(int i = 0; i < max_arr; i++) {
  59.             chance = rnd.nextFloat();
  60.             float sum = 0;
  61.             for (int j = 0; j < generated_numbers; j++) {
  62.                 sum += chance_hit[j];
  63.                 if (chance < sum) {
  64.                     ++hit[j];
  65.                     break;
  66.                 }
  67.             }
  68.         }
  69.         for(int number = 0; number < generated_numbers; number++) {
  70.             System.out.printf( "%d\t%.4f\t%d\n", number, chance_hit[number] * max_arr, hit[number]);
  71.         }
  72.     }
  73.  
  74.  
  75.     static void no_repeat(int generated_numbers) {
  76.         ArrayList<Integer> nums, temp = new ArrayList<Integer>();
  77.         Random rnd = new Random();
  78.         int it, k = 3 * generated_numbers / 4;
  79.         int hit[] = new int[generated_numbers];
  80.         int part = max_arr / k + 1;
  81.         float p;
  82.  
  83.         for(int number = 0; number < generated_numbers; number++) {
  84.             temp.add(number);
  85.         }
  86.         for(int j = 0; j < part; j++) {
  87.             nums = temp;
  88.             if (j == part - 1) k = max_arr % k;
  89.             for(int i = 0; i < k; i++) {
  90.                 p = (float) (1.0 / (generated_numbers - i));
  91.                 it = 3;
  92.                 ++hit[nums.get(it)];
  93.             }
  94.         }
  95.         for(int number = 0; number < generated_numbers; number++) {
  96.             System.out.printf("%d\t%d\n", number, hit[number]);
  97.         }
  98.     }
  99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top