In Scheme (extempore version) I sometimes use expressions similar to the following to choose alternative defined callback nodes.
(random (cons 0.1 'node1) (cons 0.2 'node2) (cons 0.7 'node3))
This will select one of the quoted symbols => node3 or node2 or node1.
In the example above, the chances of choosing node3 is greatest. https://gist.github.com/georgejwright/c480886dc85bb6b8e8d104675aa8da41#file-callback_nodes-xtm-L98
Running a test using this code:
(let pick-a-node ((i 100))
(if (= i 0)
(println "Done")
(begin
(print (random (cons 0.1 'node1) (cons 0.2 'node2) (cons 0.7 'node3)) " ")
(pick-a-node (- i 1)))))
Evaluating 100 times I got: => 12 occurrences of node1, 22 of node2 and 66 of node3.
I understand 'random' will choose one of the 3 bracketed choices. I see that somehow the 'node3 bracket has an 70% chance of being selected. But there's stuff I don't understand. Evaluating just one of these brackets on its own doesn't pick the cdr:
(cons 0.1 'node1) => (cons 0.100000 . 'node1)
nor does printing:
(print (cons 0.1 'node1)) => (cons 0.100000 quote node1)
Also notice that if the numbers such as 0.1, 0.2 and 0.7 add up to less than 1 you get an error! And if the numbers include some greater than 1.0 - such as 0.1, 4.1 and 5.7 - the first of the greater-than-ones is chosen and others ignored.
This all appears quite wierd to me.
Why does random return just the quoted element of one of them? Is there something about "random" that will do this? Are there other key words that will do the same?
I'm hoping someone is able to explain the how and why of it.