Migrate from GitHub to SourceForge with this tool. Check out all of SourceForge's recent improvements.
Close

Generators

Generators

In this section you can read about the features provided by the generator package.

Random char-, int-, float-, string generators

Often you need to generate random data. For example random integer value between 35 and 60. Or a String of the length between 5 and 10 letters. The generator package is designed to simply generate such random values.
:::java
float min = 2.25F;//min possible value which can be generated
float max = 9.75F;//max possible value which can be generated
/
* if you like to create only few values, then use simply static method of Random_1x0 class:
/
float value = Random_1x0.randomFloat(min, max);//generate value between min and max values
System.out.println(value);

/*
 * if you want to create many random values, then create an generator for this porpose
 */
Generator_1x0<Float> g = new RandomFloatGenerator_1x0(min, max);
for(int i=0; i<10; i++){
    value = g.generate();
    System.out.println(value);
}

The output after execution:

9.039439
4.740162
5.923435
6.5296235
6.9695115
6.9715204
4.995923
6.125233
3.8465247
3.096073
4.983379

Off course it works for other primitive types like char and others:

char min = 'A';//min possible value which can be generated
char max = 'D';//max possible value which can be generated
/*
* if you like to create only few values, then use simply static method of Random_1x0 class:
*/
char value = Random_1x0.randomChar(min, max);//generate value between min and max values
System.out.println(value);

/*
* if you want to create many random values, then create an generator for this porpose
*/
Generator_1x0<Character> g = new RandomCharGenerator_1x0(min, max);
for(int i=0; i<10; i++){
value = g.generate();
System.out.println(value);
}

The output after execution:

A
A
B
C
A
D
C
D
A
D
D

And now let us generate few random strings with length between 3 and 4 letters, which contains only following letters [A,B,C,D]

char minChar = 'A';//min possible value for letter which can be used
char maxChar = 'D';//max possible value for letter which can be used

int minLength = 3;//min length for generated string
int maxLength = 5;//max length for generated string

/*
 * if you like to create only few values, then use simply static method of Random_1x0 class:
 */
String value = Random_1x0.randomString(minLength, maxLength, minChar, maxChar);//generate random string
System.out.println(value);

/*
 * if you want to create many random values, then create an generator for this porpose
 */
Generator_1x0<String> g = new RandomStringGenerator_1x0(minLength, maxLength, minChar, maxChar);
for(int i=0; i<10; i++){
    value = g.generate();
    System.out.println(value);
}

The output after execution:

BADB
CCC
CDCB
BDD
ADBB
AAC
DADC
BCCB
DCAD
BDD
BDBD

Random object from collection generator

Often you want to choose randomly an object from collection. For this purpose you can use the generator which chooses randomly an object from given collection every time you call genrate() method.

//create collection with names
Collection<String> names = Arrays.asList(new String[]{"Andrej","Viktor", "Alexander", "Arnold"});

//by calling generate() method a name will be randomly chosen from predefined collection
Generator_1x0<String> g = new RandomObjectFromArrayGenerator_1x0<String>(names);
for(int i=0; i<10; i++){
    String name = g.generate();
    System.out.println(name);
}

The output after execution:

Alexander
Alexander
Arnold
Andrej
Arnold
Andrej
Arnold
Alexander
Viktor
Viktor

Composite probability generator

Sometimes the ParallelFor loop needs to execute many iterations and for every iteration will be created a new task. Too many tasks mean a lot of overhead. To avoid unnecessary overhead can be used the grain-option of Parallel.For. The grain-option defines how many iterations should be executed inside of a task, thus the Parallel.For creates few tasks, but iterates many iterations.

/**
 * create map with generators and probbilities
 */
Map<Float, Generator_1x0<String>> generatorsMap = new HashMap<Float, Generator_1x0<String>>();
generatorsMap.put(0.2F, new RandomNameGenerator_1x0(2, 5));
generatorsMap.put(0.6F, new RandomObjectFromArrayGenerator_1x0<String>(
        Arrays.asList(new String[]{"Andrej", "Viktor", "Alexander", "Elsa", "Johannes", "Arnold" , "Anton" })
        ));
//create generator and generate random strings by using two predefined generators
CompositeGenerator_1x0<String> g = new CompositeGenerator_1x0<String>(generatorsMap);        
for(int i=0; i<10 ; i++){
    String str = g.generate();
    System.out.println(str);
}

The output after execution:

Arnold
Viktor
Elsa
Alexander
Viktor
Johannes
Ca
XUUv
Anton
Andrej

Generators examples

Off course it works for other primitive types like char and others: