Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
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?

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.