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?
Are any of the provided generators uniformly random? (i.e. gen/bytes
?)
If so, I might be able to fmap
my way into a random number
Err, never mind. I might not want a uniform random number here
In any case, gen/choose is such a thing, for smaller ranges
I've thought about adding something more robust for integers and doubles
FWIW I'm generating a probability between 0.0 and 1.0, a double.
But not uniform?
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.
So an argument could be made that the most simple graph is the fully disconnected one
(i.e. the probabilities are all 0.0
Yeah that makes sense
I wonder what gen/double does in that range
It might bias toward 1 at first
If so you can fmap with #(- 1 %) to get the opposite
Yeah, with a sample of 1000 I get 101 occurrences of 1.0
and 19 occurrences of 0.0
.
That might be misleading
fmap to #(long (* 10 %)) and see what you get
It probably considers 0 and 1 simpler than everything in between
{1 39, 0 698}
Nothing from 2 to 10?
{0 700, 7 25, 1 39, 4 23, 6 14, 3 26, 2 31, 9 6, 5 36, 10 87, 8 13}
Are the full frequencies
So mostly 0, because that's simpler, and next is 1.0, then 0.5, ...
Looks like gen/double*
will do just fine
Are you actually trying to generate probabilities, or trying to generate the graph itself?
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]]
Where P1
-`P6` are probabilities. This adjacency matrix ends up representing a random DAG.
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
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.
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)
(<http://loom.io/view|loom.io/view> (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}
Cool