-
Notifications
You must be signed in to change notification settings - Fork 25
Random value
The first example shows how random values with range constraints can be created using InPUT. We use the Examples project as a starting point and play with the code in here.
The design space someSpace.xml defines a single parameter by id paramId. The parameter is a numeric parameter of type integer, and it is further defined as an array of constant size 10:
<NParam id="paramId" type="integer[10]" exclMin="-41" inclMax="42"/>
From the code, the design space is imported by calling:
IDesignSpace ds = new DesignSpace("someSpace.xml");
The IDesignSpace interface offers many methods, of which the simplest is the next method. Calling it with a valid parameter id such as in
ds.next("paramId");
it returns a random selection from the defined range, in our case an array of size 10 containing random integer values in ]-41,42]. We can play with the numbers, the attributes inclMin, inclMax, exclMin, exclMax, or even remove the MinMax constraints entirely (what happens then?). Alternatively, we could remove the array extension of the type, arriving at 'type="integer"'. Now, we expect a single integer which applies to the given space, and should therefore adjust the code appropriately to
int values = ds.next("paramId");
We could add further parameters of type float, double, short, long, or boolean, and retrieve random values in the same way.
Another alternative is to use multidimensional arrays, e.g. type="float[12][2][1][100]". Here, make sure to have the correct amount of dimensions in the assigned variable!
Often, when working with numbers, we would like to define precision or support arbitrarily large ones. If you use the numeric type classifier be prepared for BigDecimal values. E.g. for type="numeric[13]", you would have to write:
BigDecimal[] values = ds.next("paramId")
If you wish for reproducibility of the results, you can set a random seed by coding
Random rng = InPUTConfig.getValue("random");
rng.setSeed(123456789);
We can also specify in the configuration file which randomizer to use from an extendable list of randomizers, but we take that in a later stage when we talk about structural parameters.
We learned how to simply generate random numeric values. The values can be constrained and arbitrary array sizes are supported. In the next lesson we look at how to randomly create whole designs, not only single parameters.