Older Newer
Sat, 13 Jan 2007 06:02:56 . . . . SyneRyder [Added more detailed example, explanation of srand]


Changes by last author:

Added:
= rnd =

== Syntax ==

:int rnd(int a, int b)

== Arguments ==

:a

::An integer value for the lower limit.

:b

::An integer value for the upper limit.

== Return ==

:A pseudorandom value between a and b.

== Description ==

:Returns a pseudorandom integer between a and b. If you need a more random number, you should first "seed" the random value with a call to srand first.

== Example ==

<code>

%ffp

// This example generates random RGB pixel values each

// time that it is run.

OnFilterStart: {

// We only call srand to seed the random value once

// in order to make the results more random. If we

// kept calling srand(clock()) through our code, we

// would end up with similar results for each rand

// call.

srand( clock() );

return true;

}

ForEveryPixel: {

R = rnd( 0, 255 );

G = rnd( 0, 255 );

B = rnd( 0, 255 );

}

</code>

== Also see ==

:rand, srand

== Comments ==

:Since a call to srand should ideally be placed in the OnFilterStart handler, rnd is not well suited for use in the RGB handler (unless you're happy to get the same result each time you run your filter). Try using the ForEveryPixel or ForEveryTile handlers instead.