Random behaviour

tom kelly ttom.kelly@REDACTED
Sun Sep 5 20:45:57 CEST 2010


Hello List,

I just found a bahaviour I didn't expect in OTPs random module, it's not
necessarily a bug but I thought I'd share it here all the same.

I wrote a simple random data generator using random:uniform() to help me
track a bug that was triggered by certain input data that I couldn't catch.
The minimal example is attached in demo.erl.

If you run the test function it will probably pass a few times but will
eventually hit my "bug" and fail. Whenever the bug is triggered the test
function gets a badmatch and the current process dies. The behaviour I
wasn't expecting now occurs, everytime I repeat the test function I get the
exact same sequence of numbers from random:uniform.

I can call random:uniform from the shell to move it on one step in the
sequence and probably pass another test, but when it fails again my process
dies and again I repeatedly get a repeated sequence of numbers. The sequence
from this second fail is different from the first fail so it hasn't just
gone back to the start.

I don't expect to be saved from my own bugs but just thought this was a
strange behaviour from random. If anyone is using random in their
application they should be aware of this.

//Tom.


8> demo:test_my_buggy_code().
ok - [15,21,70,16,56,22,46,43,1,57]
ok
9> demo:test_my_buggy_code().
ok - [41,31,6,58,99,34,19,21,4,89]
ok
10> demo:test_my_buggy_code().
ok - [83,33,26,81,2,3,5,6,99,57]
ok
11> demo:test_my_buggy_code().
** exception error: no match of right hand side value
[1,39,67,88,90,80,59,30,
                                                       72,41,80]
     in function  demo:test_my_buggy_code/0
     in call from random:uniform/0
12> demo:test_my_buggy_code().
** exception error: no match of right hand side value
[1,39,67,88,90,80,59,30,
                                                       72,41,80]
     in function  demo:test_my_buggy_code/0
     in call from random:uniform/0
13> random:uniform(1).
1
14> demo:test_my_buggy_code().
ok - [67,88,90,80,59,30,72,41,80,8]
ok
15> demo:test_my_buggy_code().
ok - [60,68,65,8,27,61,1,94,82,65]
ok
16> demo:test_my_buggy_code().
ok - [47,2,62,94,25,97,94,87,27,32]
ok
17> demo:test_my_buggy_code().
ok - [59,21,10,14,17,24,93,78,34,28]
ok
18> demo:test_my_buggy_code().
** exception error: no match of right hand side value
[1,3,59,96,93,32,99,34,
                                                       60,42,2]
     in function  demo:test_my_buggy_code/0
     in call from random:uniform/0
19> demo:test_my_buggy_code().
** exception error: no match of right hand side value
[1,3,59,96,93,32,99,34,
                                                       60,42,2]
     in function  demo:test_my_buggy_code/0
     in call from random:uniform/0
20> demo:test_my_buggy_code().
** exception error: no match of right hand side value
[1,3,59,96,93,32,99,34,
                                                       60,42,2]
     in function  demo:test_my_buggy_code/0
     in call from random:uniform/0
21>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20100905/b69154ff/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: demo.erl
Type: application/octet-stream
Size: 320 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20100905/b69154ff/attachment.obj>


More information about the erlang-questions mailing list