1. Computing & Technology
Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link Back

Delphi Programming RTL Reference|By Category|Alphabetically|By Unit
 

RandomFrom

unit
MathStrUtils
category
random number

declaration
function RandomFrom(const AValues: array of Double): Double; overload;
function RandomFrom(const AValues: array of Integer): Integer; overload;
function RandomFrom(const AValues: array of Int64): Int64; overload;
function RandomFrom(const AValues: array of string): string; overload;

description
Returns an element at random position from an array.

Note: when providing a "number" array and expecting a number as a result, you must use the Math unit. When working with string values you must use the StrUtils unit. To initialize the random number generator, call (once) the Randomize procedure.

example
var
  Years : array of integer;
  aYear : integer;

SetLength(Years,6);
Years[0] := 1973; Years[1] := 1990; Years[2] := 1998; 
Years[3] := 2000; Years[4] := 2003; Years[5] := 2008;

Randomize;
aYear := RandomFrom(Years);

in real code

see also
Randomize, RandomRange, Random,


 Free Delphi code snippet inside every Delphi Newsletter!
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?

Discuss in my forum

©2012 About.com. All rights reserved.

A part of The New York Times Company.