-->

Randomly skip 'X' percentage of words from

2019-08-02 05:08发布

问题:

I have some java code as :

   String line = value.toString();
   StringTokenizer tokenizer = new StringTokenizer(line);

   while (tokenizer.hasMoreTokens()) {
         // do someything   
   }

However, I want the code to randomly skip X percentage of tokens.

Example : If tokens are [a , b , c , d] and skip percentage is 50% Valid execution could be printing any two tokens, say [ b , c ] or [a , d] etc

How can I implement it in the simplest manner?

回答1:

First Solution:

double percentage = 50.0;
int max = (int)percentage * token.length;

int[] skip = new int[token.length];
int count = 0;
while(count < max)
{
    int rand = rnd.nextInt(token.length);
    if(skip[rand] == 0){
        skip[rand] = 1;
        count++;
    }
}

//Use a for loop to print token where the index of skip is 0, and skip index of those with 1.

You may consider this. Create a 1D array of switches (Can be boolean too). Generate 1D array of random switches with size similar to token length. Print token element if switch of the corresponding index is true, else don't print.


Second solution:

Convert your token of array to an arrayList.
int count = 0, x = 0;

while(printed < max){  //where max is num of elements to be printed

    int rand = rnd.nextInt(2); //generate 2 numbers: 50% chance

    if (rand == 0){
        System.out.println(list.get(x);
        list.remove(x);
        printed ++;
    }
    x++;
}

Roll a probability (e.g. 50% chance) whether to print current element for every iteration. Once element is printed, remove it from list, so you won't print duplicates.


Third solution:

Randomly remove a percentage (e.g. 50%) of elements from your token. Just print the rest. This is probably one of the most straight forward way I can think of.



回答2:

first calculate the amount to skip i.e. (.50)*tokens.length (note thats pseudo code)

Then I would create an array of length tokens.length and fill it with the selected amount of 1's and the rest 0's

i.e. for 50% of 10 [1,1,1,1,1,0,0,0,0,0]

Then do a simple shuffle algorithm (Random shuffling of an array)

to get something like [0,1,1,0,0,1,0,1,1,0]

Then as you run through your tokenizer loop walk throught this array and check

(if thisArray[i]==1){
  print(token);
}


回答3:

The following uses Floyd's subset selection algorithm to select a random subset of specified size. This may be overkill for a small number of tokens, but it's pretty darned efficient for larger sets.

import java.util.HashSet;

public class FloydsSubsetSelection {

   /*
    * Floyd's algorithm to chose a random subset of m integers
    * from a set of n, outcomes are zero-based.
    */
   public static HashSet<Integer> generateMfromN(int m, int n) {
      HashSet<Integer> s = new HashSet<Integer>();
      for (int j = n-m; j < n; ++j) {
         if(! s.add((int)((j+1) * Math.random()))) {
            s.add(j);
         }
      }
      return s;
   }

   public static void main(String[] args) {
      // Stuff the tokens into an array.  I've used chars,
      // but these could be anything you want.  You can also
      // store them in any container which is indexable.
      char[] tokens = {'a', 'b', 'c', 'd', 'e', 'f'};
      int desired_percent = 50;     // change as desired

      // Convert desired percent to a count.  I added 1/2 to cause rounding
      // rather than truncation, change if different behavior is desired.
      int m = (int) (((desired_percent * tokens.length) + 0.5) / 100.0);
      HashSet<Integer> results = generateMfromN(m, tokens.length);
      for (int i: results) {                 // iterate through the generated subset
         System.out.print(tokens[i] + " ");  // to print the selected tokens
      }
      System.out.println();
   }
}


回答4:

 String line = value.toString();
   StringTokenizer tokenizer = new StringTokenizer(line);
   double percentage = 1.0 / 0.5 // replace 0.5 with the percentage you want
   int x = 0;
   while (tokenizer.hasMoreTokens()) {
         ++x;
         if (x >= percentage) {
              // print here
              x = 0;
         }
   }


标签: java random skip