Function RandomInt() Foundation

Calculate a random numeric value

Syntax
RandomInt( [<nInt>[,<nInt2>]] ) --> nRand
Parameters
<nInt>
The optional numeric parameter <nInt> states the range of 1 to <nInt> from which a random number shall be calculated.
<nInt2>
If the optional numeric parameter <nInt2> is passed, then the first parameter <nInt> is mandatory. The return value is a random number in the range of <nInt> to <nInt2>.
Return

The parameters determine a range from which a random numeric value is returned. If the function RandomInt() is called without any parameters then the return value is 0 or 1. If only the first parameter is passed then the return value is a numeric value of the range 1 to <nInt>. If the second parameter <nInt2>is passed then the first parameter is mandatory and the return value is of the range <nInt> to <nInt2>.

Description

With the function RandomInt() a numeric value can be calculated that follows stochastic (random) principals. This can be useful to fill database fields with random values and to set up unit tests where coincidence shall play a role.

The algorithm used is based on a pseudo random generator. This kind of generator always creates the same sequence of evenly distributed random numbers when it is initialized with the same seed. However, in Xbase++ the random number generator is automatically initialized on each thread by using the operating system's cryptoprovider.

A cryptoprovider is available on Windows XP / Windows 2003 and higher. On older operating systems the seed for initializing the generator is created based on time, process ID and thread ID.

Examples
Floating point numbers with RandomInt()
// Use RandomInt() to create 10 random floating 
// point numbers in the range from 0 to 1 with 
// 3 decimals 
PROCEDURE Main() 
   LOCAL n 
   // Display 3 decimals 
   SET DECIMALS TO 3 
   // Create random number from 0.000 to 1.000 
   FOR n := 1 to 10 
      ? RandomInt( 0, 1000 ) / 1000 
   NEXT 
   WAIT 
RETURN 
Use RandomInt() to paint a fractal
#define NUM_FRACTAL 4 
#define NUM_POINTS  5000 
#define NUM_EDGE    3 
PROCEDURE Main() 
   LOCAL aThread 
   CLS 
   // Start several threads which execute the function Fractal 
   aThread := Array(NUM_FRACTAL) 
   AEval( aThread, { |t| t := Thread():new() }, , , .T. ) 
   AEval( aThread, { |t| t:start("Fractal") } ) 
   ThreadWaitAll( aThread ) 
   wait 
RETURN 
// 
// Paint a fractal 
// 
PROCEDURE Fractal() 
   LOCAL aEdges, TmpEdge, OldPoint, k, n 
   // Three point in the CRT Window is the triangle 
   aEdges := Array( NUM_EDGE ) 
   AEval( aEdges, { |e| e := RandPoint() }, , , .T. ) 
   // Set the starting point 
   OldPoint := RandPoint() 
   GraLine( , OldPoint, OldPoint ) 
   FOR n := 1 to NUM_POINTS 
      // Set a color 
      GraSetColor(, RandColor()) 
      // Any point of the of the triangle 
      TmpEdge := aEdges[RandomInt(NUM_EDGE)] 
      // New Point is half the way to the edge 
      OldPoint[1] := (TmpEdge[1]+OldPoint[1])/2 
      OldPoint[2] := (TmpEdge[2]+OldPoint[2])/2 
      // Draw Point 
      GraLine(, OldPoint, OldPoint) 
   NEXT 
RETURN 
// Return a point at a random position 
STATIC FUNCTION RandPoint() 
   STATIC aSize := NIL 
   IF aSize == NIL 
      aSize := SetAppWindow():currentSize() 
   ENDIF 
RETURN {RandomInt(0,aSize[1]),RandomInt(0,aSize[2])} 
// Create a color with random RGB values 
STATIC FUNCTION RandColor() 
   LOCAL nColor 
   nColor := GraMakeRGBColor( { RandomInt(0,255), ; 
                                RandomInt(0,255), ; 
                                RandomInt(0,255)}) 
RETURN nColor 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.