How to test properties of random generator - scala

Using Scala, I've a method that return a set of 5 random numbers, that should be between 1 and a constant LIMIT.
What's the best approach to test that a answer will never return more/less than 5 elements, and all elements are between 1 and LIMIT? Making a simple test is easy. But should I make a loop of, lets say, 1000 iterations to better test it? Or there is some feature in unit testing for such cases?
Using Scala and ScalaTest.FunSuite

Take a look at https://softwareengineering.stackexchange.com/questions/147134/how-should-i-test-randomness
My approach would be to generate 100 sets for the limit 20 and test if the occurrences of each number are nearly equally divided.

Let's try QuickTheories - property based testing framework.
It runs tests with many different possible generated input.

Related

Optimising first solution strategy for VRP

I'm trying to pick the best first solution strategy to use on a VRP.
My use case is that an individual case takes around 60 seconds to solve on average, but i need to run hundreds or thousands of cases sequentially such that my whole solution takes hours.
I can trade off finding the optimal solution against time; a good solution is usually good enough.
Using the different strategies, i get solve times between 1 and 120 seconds.
My questions:
Is it reasonable to assume that the best strategy for one case will also be the best for other cases given my model does not change much - just different pickup nodes and time windows?
Has anyone tried first testing each strategy then picking the best to use for the rest of the cases?
If i was to set the limit time to e.g. 1 second, would the strategy that gives the lowest objective function after say 1s also be likely to give the best solution strategy after 60s, unlimited?
Many thanks!

It was my interview question write MC/DC for (A&&B)||A

How many test cases will be for this boolean expression?
If I follow MCDC rules I am getting only two test cases
(A&&B)||A decision
1 T. F. T. T
2 F. T. F. F
at least one true true combination for AND gate I don't know how make without breaking other rules.
I believe you need to create a minimum of 10 test cases. Firstly, there are two conditions; A & B, for which you need two cases each. Means a total of 4 here. Secondly, you need to create two test cases for each combination of conditions, which means an additional 4 test cases. And lastly, you would need to create two test cases for the overall decision made by the expression which will be again for a total of 2 test cases. that means it will be a total of 10 Test Cases.

The best threshold for a Mergesort to switch to an insertion sort at?

I've been working on a piece of java code to determine the best threshold for a mergesort to switch to insertion sort at and my results have been less than satisfactory.
The tests I'm running take nearly an hour and produce data which doesn't really represent any particular pattern to me. So I'm hoping to ask what I should expect for the best threshold. Should it be constant? Should it be N/(some number)? is it constant after a certain N value? Roughly what would you expect?
(if it matters I am comparing Integer objects in java)
It depends somewhat on your actual hardware.
The best approach is to benchmark on your target hardware.
It's usually between 10 and 50, but test between 10 and 100.
In implementations I worked on some time ago, the threshold was 22 items.

Chi-sqaure type-1-error

I have a question about the chi-square test.
I have two between-subject factors, each with two levels (so 4 conditions). Furthermore, I have one dependent variable (qualitative), also consisting of two levels.
Now I want to make pairwise comparisons (so I have 6 chi-sqaure test in total). Is there any way I can control type-1-errors? In the literature I saw they often calculated interaction with a chi-sqaure test. Is this the way to do it, and if so, how do I do it?
I can work with both SPSS and Matlab.
Thank in advance!
Niels

How can I use reproducible randomization in Perl?

I have a Perl script that uses rand to generate pseudorandom integers in some range. I want it to be random (i.e. not set the seed by myself to some constant), but also want to be able to reproduce the results of a specific run if needed.
What would you do?
McWafflestix says:
Possibly you want to have a default randomly determined seed, that will give you complete randomness when desired, but which can be set prior to a run manually to give reproducibility.
The obvious way to implement this is to follow your normal seeding process (either manually from a strong random source, or letting perl do it automatically on the first call to rand), then use the first generated random value as the seed, and record it. If you want to reproduce later, just use a recorded value for the seed.
# something like this?
if ( defined $input_rand_seed ) {
srand($input_rand_seed);
} else {
my $seed = rand(); # or something fancier
log_random_seed($seed);
srand($seed);
}
If the purpose is to be able to reproduce simulation paths which incorporate random shocks (say, when you are running an economic model to produce projections, I would give up on the idea of storing the seed, but rather store each sequence alongside the model data.
Note that the built in rand is subject to vagaries of the rand implementation provided by the C runtime. On all Windows machines and across all perl versions I have used, this usually means that rand will only ever produce 32768 unique values.
That is severely limited for any serious purpose. In simulations, a crucial criterion is that random sequences used be independent of each other so that each run can be considered an independent realization.
In fact, if you are going to run a simulation 1,000 times, I would pre-produce 1,000 corresponding random sequences using known-good generators that are consistent across platforms and store them with the model inputs.
You can update the simulations using the same sequences or a new set if parameter estimates change when you get new data.
Log the seed for each run and provide a method to call the script and set the seed?
Why don't you want to set the seed, but at the same time set the seed? As I've said to you before, you need to explain why you don't want to do something so we know what you are actually asking.
You might just set it yourself only in certain conditions:
srand( $ENV{SOME_SEED} ) if defined $ENV{SOME_SEED};
If you don't call srand, rand calls it for you automatically but it doesn't report the seed that it used (at least not until Perl 5.14).
It's really just a simple programming problem. Just turn what you outlined into the code that does what you said.
Your goals are at odds with each other. One one hand, you want a self-seeding, completely random sequence of integers; on the other hand, you want reproducibility. Completely random and reproducibility are at odds with each other.
You can set the seed to something you want. Possibly you want to have a default randomly determined seed, that will give you complete randomness when desired, but which can be set prior to a run manually to give reproducibility.