Messages from list : cormas@cirad.fr

Choose a topic among the following archives :

Re: Cormas random

New Message Reply Date view Thread view Subject view Author view

Subject: Re: Cormas random
From: Pierre (bommel@cirad.fr)
Date: Wed Aug 12 2009 - 00:01:22 CEST

Hello Jean-Emmanuel,

Cormas random answers a pseudo-random value between ]0;1], according to
the RandFishmanMoore equation.
So, in 1000000 tests, there is some chance to get 1.0, but never 0.0.
Example : doing the following code :
| i num |
 i := 0.
10000000 timesRepeat: [i := i + 1. num := (CormasNS.Kernel.Cormas
random*3).
num = 3 ifTrue:[self halt].
num = 0 ifTrue:[self halt]].

the run will halt after a very high number of loops (3136338,
4786337,... in my cases), but won't stop for 0.
If you only want random integers between [0 ; 3[, I propose this :
    (Cormas random * 2.99999) ceiling
ceiling "Answer the integer nearest the receiver toward positive
infinity."

If you put 3 instead of 2.99999, you will have 4 some (very few) times.
This is due to floating point arithmetic errors (see :
http://www.macaulay.ac.uk/fearlus/floating-point/index.html) : some
times the result may be 3.0 (Float) but it could be 3.0000000827d in
double precision, then ceiling answers 4 !
Testing for 10 M loops :
| integer result num |
    result := Array withAll: #(0 0 0).
10000000 timesRepeat: [num := (CormasNS.Kernel.Cormas random * 2.99999).
integer := num ceiling.
result at: integer put: (1 + (result at: integer))].
CormasNS.Kernel.Cormas println: result printString.

'''#(3333565 3333132 3333303)'''
'''#(3332332 3333208 3334460)'''
 If you change 2.99999 for 3, you will have a "subscript out of bounds"
exception, one time every 1000000 loops...

Best regards,
    Pierre

Jean-Emmanuel Rougier a écrit :
>
> Hi All,
>
>
>
> I do apologize in advance if my question is a newbie one, but as I am
> totally new in the fantastic world of Cormas...
>
>
>
> I am using the Cormas random procedure in order to have random numbers
> (yes).
>
> So, I use exactly the formula : (Cormas random*n) truncated.
>
> To obtain uniform distribution of random numbers from the sequence
> {0,1,2,...,n-1}
>
> Of course, as Smalltalk is full of tricks for a newcomers that comes
> from C/C++, I tested it.
>
> And some time to time I obtain n (in my tests, 3 for 1000000 calls).
>
> Which is totally impossible.
>
> So...
>
> Where is the trick?
>
>
>
> Thanks !
>
>
>
> Jean-Emmanuel Rougier,
>
> Associé-Gérant,
>
>
>
> logo-lisode-petit <http://www.lisode.com/>
>
>
>
> 361 rue JF Breton
>
> BP 5095
>
> 34196 Montpellier Cedex 5
>
> www.lisode.com <http://www.lisode.com/>
>
> +33 (0) 4 67 04 63 51
>
> +33 (0) 6 62 67 51 33
>
>
>

-- 
--------------------------------
Pierre Bommel - CIRAD - UR Green
UnB - Faculdade de tecnologia.
Departamento de engenharia mecânica
Campus universitário Darcy Ribeiro
Asa norte   	Brasília - DF
tel : (00 55 61) 3208 3405
cel : (00 55 61) 8122 2878
http://www.cirad.fr/ur/green
-------------------------------- 

New Message Reply Date view Thread view Subject view Author view
 

Back to home