PDA

View Full Version : IRAND problem



cameyo
01-02-05, 06:48 AM
Hi all,
i have a problem with the IRAND function...
The following zscript executes IRAND(9) <iterations> times.
For each iteration the script add "1" to the relative vector MoveList(Random Value).
In this way MoveList(i) contains, at the end, how many times the value "i" was been selected (for i=0 to 9).
With 5000 iteration i suppose to see each MoveList(i) with a value of 500 (+/-)...
...but i have the following result:
Number of "0" extracted: 277 (*** strange value ***)
Number of "1" extracted: 553
Number of "2" extracted: 553
Number of "3" extracted: 561
Number of "4" extracted: 555
Number of "5" extracted: 552
Number of "6" extracted: 566
Number of "7" extracted: 539
Number of "8" extracted: 577
Number of "9" extracted: 267 (*** strange value ***)
I have tried again and again, but with the same result (i got different values, but with the same trend to have the first and the last value very strange)
Clearly, the IRAND is NOT uniform !!! (or i have made some mistakes :))
I hope the script will clarify the situation.

The question
Is there some mistakes in my script or IRAND is not uniform ?

Thanks for viewing...and for helping.

cameyo

marcus_civis
01-02-05, 07:55 AM
Hi cameyo,

I know nothing about Math functions :) but RAND seems to work ok:

[VarSet,direction,RAND(10)]
[If, (direction < 0) || (direction > 10), [Note,"Error"]]

(I changed your code a little...) so perhaps something wrong with IRAND?

Marcus

Svengali
01-02-05, 02:11 PM
Hi Cameyo,

As a test, substitute the following lines and run the script:

[VarSet,direction,IRAND(0)+5]
and
[VarSet,direction,IRAND(1)+5]
and
[VarSet,direction,IRAND(2)+5]

IRAND does an integer rounding operation and I think the distributions are as you'd expect, aren't they?

Sven

cameyo
01-02-05, 10:43 PM
Thanks Marcus and Svengali.
I suspect the first and the last number of IRAND range don't have...the 0.5 of previous (or next) number (as Svengali point out).
I have tried your hint, but i don't think the distribution is uniform.
I have write a graphic routine to trace out the error (the "drunk" walking), and the mistery is not solve ...(maybe Aurick or Pixolator) :)
Using RAND is my last chance ;)

cameyo

Pixolator
01-02-05, 11:50 PM
Hi:)
cameyo: Yes, it is the start/end .5 which are responsible for the uneven IRAND distribution that you are getting. When using IRAND(9) you get 9 equally distributed results, not 10 (the first .5 and last .5 are of the same circular range).
You should be able to achieve a uniform distribution of 0-9 by using INT(RAND(10)).
-Pixolator

cameyo
01-03-05, 03:23 AM
Thanks Pixolator.
I have tried your hint (INT(RAND(x)), with the following result (1000 iterations):
Number of "0" extracted: 1022
Number of "1" extracted: 986
Number of "2" extracted: 1059
Number of "3" extracted: 950
Number of "4" extracted: 984
Number of "5" extracted: 1012
Number of "6" extracted: 1041
Number of "7" extracted: 1025
Number of "8" extracted: 971
Number of "9" extracted: 950
All works fine, but...
Now i have write a small "Walking Drunk" routine with the following result (50.000 iterations):
Image1.jpg
The routine starts drawing an object in the center of the canvas and move (one step) random in one of eight direction (N, NE, E, SE, S, SW, W, NW), then move random for the number of iterations.
The image shown a regular pattern.... :o... not a random pattern.
The word of pseudo, random, aleatory number is very misterious...Tonight (i hope) i'll write a personal random number generator and i'll post the script to do some tests.
Now you can try with the Zplace2 revI beta2 version: set the parameteres and press the "W" button...

Thanks for trying.

cameyo

RVy
01-06-05, 08:29 AM
It is possible to use the Congruent Pseudo-Random Number Generator (PRNG) x(i+1) = k*x(i) mod p.
For example good results give p= 5087, k=2900. But it is better to find big p & k that the big period of not repeating sequences of numbers was.
Look an example for numbers 5087 and 2900 in a script WALK2-RVy.txt.

Frenchy Pilou
01-06-05, 09:08 AM
At Start we have...
N = something between 0 and 1 :b3: like 0.2561
Pi = that the computer has in his memory :b3: Maybe 3.14159265358979 .... ;)
^ = Power of a number : Example 3 ^ 5 = 3 * 3 * 3 * 3 * 3 = 243
Ent = the Entire part of a decimal number : 3 is the entire part of 3.14

*** Pseudo code ***
:b4: N = [( N + Pi ) ^ 5 ] - Ent [( N + Pi ) ^ 5 ]
Print N
Goto :b4:

Is not marvelous and simple ? :D
You will obtain a regular uniform distribution randomizing :cool:
0.814636029
0.185563304
0.723545765
0.631390691
0.582652538
0.458977266
0.140534350
0.870127423
0.089865958
0.365182693
...
At the end free to you to multiply by 10,100,... and take the entire part to obtain
8
1
7
6
5
4
1
8
0
3
...
81
18
72
63
58
45
14
87
8
36
...
Pilou

cameyo
01-09-05, 04:12 AM
Thanks all for reply.
I had some problem with the internal math precision of Zscript:evil:
I have written some more code to test the rand generator...
Download the Zplace2 revI b4
The first example show basic method:
Start ZBRUSH
Load Zplace2
Select Sphere 3D
Set DrawSize to 5
Press the "Snap" switch
Open the "Walk" section
Press Ctrl-D (place Sphere3D on canvas center)
Now select this color R= G= B=
Set _iterations_ to 10 (10.000 iterations)
Press "Randomize" button (To start with new seed)
Press "ShowInfo" switch
Now press "Drunk Walking" button (or press Ctrl-W)
You can see the sphere3D random movements on canvas...
You can stop the walking pressing 'ESC', you can restart walking pressing 'Ctrl-W'.
In the second example we start another walking, but with a new property: each time the color of object changes to reflect how many times that place was reached:
Start ZBRUSH
Load Zplace2
Select Sphere 3D
Set DrawSize to 5
Press the "Snap" switch
Open the "Walk" section
Press Ctrl-D (place Sphere3D on canvas center)
Now select this color R=255 G=255 B=255 (white)
Set _iterations_ to 10 (10.000 iterations)
Set _sColorR_ = _sColorG_ = _sColorB_ = -10 (grayscale). You can see the selected colors in the palette.
Press "Randomize" button (To start with new seed)
Press "ShowInfo" switch
Now press "Drunk Walking" button (or press Ctrl-W)
You can see the sphere3D random movements on canvas...with different color where is located multiple placements.
Some hints:
Try with different objects (3D, 2D 2.5D tools), different _MoveX and _MoveY values, different color ramp, etc
You can overlay two or more "walking" with different Z...then render with shadow
To obtain a 'regular walking' you must have _MoveX = _MoveY = DrawSize/2
If _sColorR_ = _sColorG_ = _sColorB_ = 0 the 'walking' is monochromatic.
You can stop the 'walking' by pressing 'ESC, press 'w' to show the GYRO, move the object and restart 'walking' with 'Ctrl-W'.
Use a 4096x4096 canvas with Drawsize=8 and _MoveX = _MoveY = 4 for best result.
With high mumber of iterations the procedure will draw strange regular pattern...

Here is some examples:
Brownian06_show.jpg

Brownian07_show.jpg

Thanks for try.

cameyo

p.s. sorry, i have some problems to upload images:confused: i'll try later...

aminuts
01-09-05, 12:19 PM
cool you just gave me an idea for creating a random landscape...now I will have to test it in revI. :D

Frenchy Pilou
01-09-05, 12:37 PM
If you take my pseudo above, no doubt that you will obtain a random landscape without artefact :)
A random walk or a map by colors (in this last case no doubt that you obtain a foggy altitude image :rolleyes:
Pilou

aminuts
01-09-05, 01:02 PM
:D Frenchy!!

froyd
01-09-05, 01:13 PM
nice patterns! looking forward to the rand solution! ;)

greets
froyd

cameyo
01-09-05, 10:54 PM
Hi all,
thanks for reply...
Frenchy: i'll try your random generator tomorrow. Thanks.
RVy: thanks for your PRNG. I'll try it too. I have read your zOthello script...very interesting.
The script will allow to select the random generator. I have written a random generator, but the range of floating number is so small :cry:
Anyway, i think every pseudo-generator will generate a repetitive drawing...

cameyo

cameyo
01-09-05, 11:03 PM
...

cameyo
01-09-05, 11:10 PM
...

Frenchy Pilou
01-10-05, 04:22 AM
Damned! You obliged me to re-prog :D
Test with a sympatico free Prog :tu: (For Windows and Unix and Linux and PS2:eek: )
(only default for the moment : He has no color(just Black & white in Drawing mode) :(
In text mode yes some colors ;)
yaBasic (http://www.yabasic.de/) :cool: (Yet An Another Basic) :lol:
If you use it, Install it, use your favorite Editor, and make careful at that :ex: a "A" is different of a "a" :ex: He he :rolleyes:
Just drawing a line with Startline and Endline with Pilou randomizing
Something like that :) (it's not pseudo code this time, it's real code :)

open window 1040,778 ......................................// open a graphic window
N = 0.2561 .................................................. ...// initialize N to a number <0
for T=1 to 2000.............................................. .// draw 2000 lines
N = (( N + pi ) ^ 5 ) - int(( N + pi ) ^ 5 ) ............// the Pilou randomize system (thx Jp-Petit)
X = int(N*1040) .............................................// Xposition of the Startline
N = (( N + pi ) ^ 5 ) - int(( N + pi ) ^ 5 )
Y = int(N*778) ...............................................// Yposition of the Startline
N = (( N + pi ) ^ 5 ) - int(( N + pi ) ^ 5 )
X1 = int(N*1040) ...........................................// Xposition of the Endline
N = (( N + pi ) ^ 5 ) - int(( N + pi ) ^ 5 )
Y1 = int(N*778) ............................................// Yposition of the Endline
line X,Y,X1,Y1 ..............................................// draw a line
next T
Seems uniform and regular ;)
random.jpg
Pilou
ps You can make a "Stand Alone" like this One (the above prog) (http://www.zbrushcentral.com/zbc/attachment.php?attachmentid=4745&stc=1) (for Windows : an ".exe")
Pss Compiling is also possible!
And an Editor exist YabEdit (http://www.rovoscape.com/yabasicrealms/yabedit.php)

Gary Komar
01-10-05, 07:38 AM
keyboard broke? good to see from you again,more info to come?

cameyo
01-10-05, 10:40 AM
Hi all,
Frenchy: the problem is not with your random generator, but with the internal math of zscript...in fact the range of floating number is very small.
I have developed a new version of 'drunk walking' with some new images.
I will post it tomorrow...now i have a slow internet connection :cry:

Gary Komar: how are your beatiful virtual girl ?

cameyo

marciani
01-10-05, 03:09 PM
...Cameyo , Frenchy, Gary ,you have all my admiration ......

it's happened everytime i don't understand somethingh (often:D ).
For me what are you talking about is like Greek or chinese.........
bye
Leo

Frenchy Pilou
01-10-05, 04:54 PM
@Leo
...and chiness are also some darky for me :D
@Cameyo
If the range is too small, just add (past)artificialy 2 or more existant ;)
So time to make a complet turn will be biggest than the Univers age :D
Pilou

cameyo
01-10-05, 10:45 PM
Hi all,
i have done some tests...

The first test is with the internal random generator (RAND).
It works quite well (see attached image). The sequence is visible, but it has big length.

The RVy routine (PRNG) is very good.
I have tried with (p=5087, k=2900) and (p=2027, k=1298) (see attached images too).
RVy1.jpg
I wish to known a method to find good p and k values ;)

The Frenchy Pilou routine have some problems...because the range of floating number is very small within Zscript :(
I have attached an image of result with this routine. The sequence is very short.
I had the same problem when i tried to write this function in zscript language:
----------------------------------------------------------
/* uniform [0,1] random number generator
developed by Pierre Lecuyer based on a clever
and tested combination of two linear congruential
sequences
*/
/*
s1 and s2 are the seeds (nonnegative integers)
*/
#include <math.h>
double uni()
{
static long s1 = 55555;
static long s2 = 99999;
static double factor = 1.0/2147483563.0;
register long k,z;
k= s1 /53668;
s1 =40014*(s1%53668)-k*12211;
if (s1 < 0) s1 += 2147483563;
k=s2/52774;
s2=40692*(s2%52774)-k*3791;
if (s2 < 0) s2 += 2147483399;

/*
z = abs(s1 ^ s2);
*/
z= (s1 - 2147483563) + s2;
if (z < 1) z += 2147483562;

return(((double)(z))*factor);
}
------------------------------------------------------------
In zscript 1.0/2147483563.0 got wrong value...

I have attached the ZPlace2 RevIb5 (with source ;)) for anyone who want to play.
Next i'll write another Random Generator routine from Knuth book.

Thanks for viewing

cameyo

p.s. Unless mistakes all works fine :)

Frenchy Pilou
01-11-05, 08:34 AM
...was a too simple walk ;)
Some repetitive artefact seems arrive but...
it's just a question of parameters
if you take the same
N= 0.2561 and euler =2.71828182864
and now N = (( N + euler ) ^ pi ) - int(( N + euler ) ^ pi )
You will obtain a drunk walk with a mysterious strange attraction towards the up screen and without repetition :D
(tested to 50 000 000 iterations) not more because my poor basic is a little slow :rolleyes:
Pilou
Ps Interesting links about Random number! (http://www.iro.umontreal.ca/~simardr/random.html#Rand)
Another one! (http://random.mat.sbg.ac.at/links/rando.html)

Frenchy Pilou
01-12-05, 12:54 PM
(RvY similar Pseudo Random Number Generator? )
Derrick Henry Lehmer's (http://www.e-paranoids.com/d/de/derrick_henry_lehmer.html) generator 1905-1991 (thx Poucet)
U = ( A * Un-1 ) Mod B
A and B prime numbers
Un-1 = germ (seed) <> 0
You will obtain
each and just one each number between 0 and B-1 :eek: :eek: :eek: magic :lol:

U = (16807 * Un-1 ) mod 2147483647
or more small
U = ( 16011 * Un-1 ) mod 65521

if the modulo function has some limits use that! (Pseudo Code)
Function Alea (U)
A = 16807 * U
B = 2147483647
Alea = A-(int(A/B)*B)
End Function

Zscript accept 2147483647 ?
Pilou
Ps Seems a joyous company :) Not repetitive before (2 147 483 647 - 50 000 000) :lol:
Sans titre - 1.gif
Source (for yaBasic (http://www.yabasic.de/) :cool: (Yet An Another Basic) :lol: Pc Windows - Text file)

cameyo
01-12-05, 10:59 PM
Frenchy,
this works better !!!
Very good number generator.
I'm working on an'idea of using the decimal part of Pi Greek (3.1415...) to random walk.
I have the first 100.000 numbers and i'm trying to find a method to code it with zscript...
I have write a small delphi program to try the Knuth generator (see. vol.2 of his books) and i must admit it is one of the best.
I'll post the image of Pi greek walking...later...maybe.

cameyo

cameyo
01-13-05, 04:46 AM
Hi all,
here is the walking with Pi Greek...
Image1.jpg
The complete image is in attachment (half size and jpg :()...
I have upload an alpha image and the first 100000 decimals.

cameyo

p.s. plus the ZPlace2 Rev.Ib6

Frenchy Pilou
01-13-05, 04:53 AM
...for the storage of your 100 000 Pi number:)
Hi Cameyo
Just draw a little colored image 316*317 = 100172 pixols with 10 coded colors :)
I suppose that Zscript is capable to read a color on the screen (key "C" for example :)
And maybe these image can be curious also :rolleyes:
Pilou
Ps He he you are very speedy this day :D
Pss What is the method to read your Alpha Image of the 100 000 decimal of Pi ? a direction = a decimal number ?

cameyo
01-13-05, 05:06 AM
Well, to store the 100000 decimals i have used...zscript !!!
I have defined an array PiLIST(100000), then i have done a small Delphi program to translate 3.1415.....into:
[VarSet,PiLIST(i),<current decimal>]
[VarInc(i)]
for each decimal.
The final zscript file (.txt) is 2.5 Mb !
To load the script my PC need 15 minutes, but i wonder how stable is ZBrush for this work.
Then i run the script...and discover the image.

Thanks my friend.

cameyo

Frenchy Pilou
01-13-05, 05:09 AM
Pss with my method of 10 Colors in Gif format for no deperdition you will obtain a file of 50 Kb so better than the file text of your 1000 000 pi Number ;)

cameyo
01-13-05, 05:15 AM
You right !!!
But to read a decimal from your color map i must use the function PixolPick and it is more,more slow than [VarSet, PiLIST(I), xxx] ;)

Often the programs are a trade-off between speed and size.

Thanks again for your idea...maybe i'll use it in another project.

cameyo

p.s your posts = my posts x 8 -----------> 8960 = 1120 x 8

Frenchy Pilou
01-13-05, 05:24 AM
How interpretate your Alpha 100 000 Image?
The drunk walk use 8 directions ?
in any case very similar image of the Lehmer generator :) same familly of aspect!
Pilou
Ps You are 8 time more serious than me :D

cameyo
01-13-05, 06:17 AM
Frenchy,
the Alpha is the MRGBZgrab of the complete Pi Greek walk.
Yes, it use 8 direction (from 0 to 7) the decimals 8 and 9 are not considered (or, better, consideread as 'pause walk').
To test the Pi you must use one million of decimals at least....

ciao

cameyo

aminuts
01-15-05, 12:01 AM
hahaha Cameyo!;)

how observant of you to notice the cameyo posts x8 = pilou / 8 = Cameyo.

You must rest my friend too many numbers on the brain!!;)