1 | :- multifile generate/2. | |
2 | :- multifile shrink/3. | |
3 | ||
4 | :- use_module(library(random),[random/3]). | |
5 | :- use_module(library(sets),[subset/2]). | |
6 | ||
7 | % Options: | |
8 | % small, positive, negative, nozero, between(A,B) or none | |
9 | ||
10 | generate(integer(Options),Value) :- | |
11 | member(between(A,B),Options) , | |
12 | generate(between(A,B),Value). | |
13 | ||
14 | generate(integer(Options),Value) :- | |
15 | subset([small,positive,nozero],Options) , | |
16 | !, | |
17 | random(1,128,Value). | |
18 | ||
19 | generate(integer(Options),Value) :- | |
20 | subset([small,positive],Options) , | |
21 | !, | |
22 | random(0,128,Value). | |
23 | ||
24 | generate(integer(Options),Value) :- | |
25 | subset([small,negative],Options) , | |
26 | !, | |
27 | random(-128,-1,Value). | |
28 | ||
29 | generate(integer(Options),Value) :- | |
30 | subset([positive,nozero],Options) , | |
31 | !, | |
32 | random(1,20000,Value). | |
33 | ||
34 | generate(integer(Options),Value) :- | |
35 | member(positive,Options) , | |
36 | !, | |
37 | random(0,20000,Value). | |
38 | ||
39 | generate(integer(Options),Value) :- | |
40 | member(negative,Options) , | |
41 | !, | |
42 | random(-20000,-1,Value). | |
43 | ||
44 | generate(integer(Options),Value) :- | |
45 | member(small,Options) , | |
46 | !, | |
47 | random(-64,64,Value). | |
48 | ||
49 | generate(integer(_),Value) :- | |
50 | random(-20000,20000,Value). | |
51 | ||
52 | % try 0 | |
53 | shrink(integer(_),_,0). | |
54 | ||
55 | % divide by two for big numbers | |
56 | shrink(integer(_),Value,Shrunken) :- | |
57 | (Value > 100 ; Value < -100) , | |
58 | Shrunken is round(Value / 2). | |
59 | ||
60 | % smaller steps | |
61 | shrink(integer(_),Value,Shrunken) :- | |
62 | Value > 0 , | |
63 | Shrunken is Value - 1 . | |
64 | shrink(integer(_),Value,Shrunken) :- | |
65 | Value < 0 , | |
66 | Shrunken is Value + 1. |