lundi 15 janvier 2018

using seed in xeger java

I found a library called xeger that generates strings given a regular expression. It generates the string randomly. I have been using R for long time now. In R we just need to set seed and it generates same random number every time.

I have written the following code for generating the strings using xeger.

import java.util.Random;

    import nl.flotsam.xeger.Xeger;

    public class StringGenerator {
        public static void main(String args[]) {
            giveMeString(2);
        }

        public static void giveMeString(int x) {
            Random rnd = new Random(x);
            try {
                String regex = "[1-9]\\|[1-9][0-9]{1,5}\\|1000000";
                Xeger generator = new Xeger(regex);
                String result = generator.generate();
                System.out.println("Generated String is : " + result);
            } catch (Throwable ex) {
                ex.printStackTrace();
            }
        }
    }

I have used Random rnd = new Random(x); at the top of the giveMeString() function but still it generates random string randomly. How do I use the seed in proper way such that it generates the same string for a particular seed. I searched for examples and it says to use the seed as rnd.nextInt(i). But I want to generate a random string based on seed and not the integer. I am confused on how to do this.




Aucun commentaire:

Enregistrer un commentaire