Fork me on GitHub
#test-check
<
2018-04-14
>
nwjsmith17:04:19

I think I've asked this before, but I'm writing some (non-shrinking) generators that rely on generating uniformly random numbers. Is there any way to get a uniformly random number generated?

nwjsmith17:04:54

Are any of the provided generators uniformly random? (i.e. gen/bytes?)

nwjsmith17:04:11

If so, I might be able to fmap my way into a random number

nwjsmith17:04:35

Err, never mind. I might not want a uniform random number here

gfredericks18:04:11

In any case, gen/choose is such a thing, for smaller ranges

gfredericks18:04:26

I've thought about adding something more robust for integers and doubles

nwjsmith18:04:14

FWIW I'm generating a probability between 0.0 and 1.0, a double.

gfredericks18:04:28

But not uniform?

nwjsmith18:04:55

I don't think it needs to be. It's a generator for a graph's adjacency matrix. It is the probability that one vertex is adjacent to another.

nwjsmith18:04:20

So an argument could be made that the most simple graph is the fully disconnected one

nwjsmith18:04:32

(i.e. the probabilities are all 0.0

gfredericks18:04:34

Yeah that makes sense

gfredericks18:04:58

I wonder what gen/double does in that range

gfredericks18:04:14

It might bias toward 1 at first

gfredericks18:04:54

If so you can fmap with #(- 1 %) to get the opposite

nwjsmith18:04:42

Yeah, with a sample of 1000 I get 101 occurrences of 1.0 and 19 occurrences of 0.0.

gfredericks18:04:30

That might be misleading

gfredericks18:04:57

fmap to #(long (* 10 %)) and see what you get

gfredericks18:04:24

It probably considers 0 and 1 simpler than everything in between

nwjsmith18:04:59

{1 39, 0 698}

gfredericks18:04:49

Nothing from 2 to 10?

nwjsmith18:04:14

{0 700, 7 25, 1 39, 4 23, 6 14, 3 26, 2 31, 9 6, 5 36, 10 87, 8 13}

nwjsmith18:04:26

Are the full frequencies

gfredericks18:04:33

So mostly 0, because that's simpler, and next is 1.0, then 0.5, ...

nwjsmith18:04:41

Looks like gen/double* will do just fine

gfredericks18:04:44

Are you actually trying to generate probabilities, or trying to generate the graph itself?

nwjsmith19:04:10

Generate the graph itself. I generate an adjacency matrix like this:

[[0.0 0.0 0.0 0.0]
 [P.1 0.0 0.0 0.0]
 [P.2 P.3 0.0 0.0]
 [P.4 P.5 P.6 0.0]]

nwjsmith19:04:18

Where P1-`P6` are probabilities. This adjacency matrix ends up representing a random DAG.

nwjsmith19:04:30

I map this probability matrix into a matrix where the values can be true/`false` based on whether or not the probability is above 0.5

nwjsmith19:04:19

I have another generator that takes a DAG and generates a random topological ordering from it. It's also based on random number generation using gen/choose, but the range is small enough that it should be uniform.

nwjsmith19:04:42

Both are looking good:

(let [g (gen/generate (gen-directed-acyclic-graph (seq "ABCD")))
       t (frequencies (gen/sample (gen-topological-ordering g) 1000))]
   (when (not-empty g)
     (println t)
     ( (loom.graph/digraph g))))
Produced: https://www.dropbox.com/s/q0rkqfy7z0vffs6/png223331814782198197.png?dl=0 and the topological orderings:
{[C B D A] 122,
 [D B A C] 117,
 [B D A C] 115,
 [D B C A] 127,
 [B D C A] 135,
 [C D B A] 132,
 [D C B A] 137,
 [B C D A] 115}