/* * JFLAP - Formal Languages and Automata Package * * * Susan H. Rodger * Computer Science Department * Duke University * August 27, 2009 * Copyright (c) 2002-2009 * All rights reserved. * JFLAP is open source software. Please see the LICENSE for terms. * */ package pumping; import java.util.Random; /** * This class encapsulates many static computational methods used by the pumping * lemmas that do not necessarily need to be in the PumpingLemma class. * * @author Chris Morgan & Jinghui Lim */ public class LemmaMath { /** * A random number generator. Here, it is used to generate random boolean * and int values. */ private static Random RAND_GENERATOR = new Random(); /** * Returns 0 with probability 0.5 and 2 with probability 0.5. * This should be used to randomize whether a pumping lemma is * pumped 0 or 2 times when both will give a contradiction. * * @return 0 with probability 0.5 and 2 with probability 0.5 */ public static int flipCoin() { if(RAND_GENERATOR.nextBoolean()) return 0; else return 2; } /** * Returns a random integer between min & max (both inclusive). * * @return a random integer between min & max (both inclusive). */ public static int fetchRandInt(int min, int max){ return RAND_GENERATOR.nextInt(max-min+1) + min; } /** * Counts the number of times the character 'c' appears in s. Used * for subclasses to determine if a particular string is a valid instance * of the language * * @return # of instances of c in s */ public static int countInstances(String s, char c){ int count=0; for (int i=0; i