rand

Syntax

int rand()

Arguments

None

Return

A random number between 0 and RAND_MAX

Description

Generates a pseudorandom number between 0 and RAND_MAX, which at the time of writing is 32767. You can use the modulo operator (%) to reduce the result to a defined range. You should also call the srand function before first calling the rand function, to ensure that the numbers do seem to be random and not predefined.

Example

%ffp

// Sets each of the control values to a random
// value between 0 and 200.

ctl(0): "Red",   range=( 0, 200 )
ctl(1): "Green", range=( 0, 200 )
ctl(2): "Blue",  range=( 0, 200 )

onFilterStart: {

  srand( clock() ); 
  setCtlVal( 0, rand() % 200 );
  setCtlVal( 1, rand() % 200 );
  setCtlVal( 2, rand() % 200 );

  return false;
}

Also see

srand, rst

Comments

Everyone can add his comments about his experiences with this function here. Tips for using it are welcome, too.