Random numbers, strings and wordsIn today's column, we have a detailed look at the function random. Delphi provides a random number generator, that generates pseudo-random numbers. Random numbersThe function random(n) returns a random integer number within the range 0 <= X < n. To initialize the random number generator, add a call to randomize before making any calls to random(). Random stringsFor a random string, we start from a "seed" string, containing all the possible characters that we want to appear in our random string. Next, we generate some random numbers N, and for each of them we add character number N: SeedString := 'abcdefghijklmnopqrstuvwxyz'; S = ''; for (i := 1 to 6 do begin // string of 6 characters N := Random(Length(SeedString)) + 1; S := S + SeedString[N]; end; Random wordsFor a random word, we start from a "seed" array of strings, containing all the possible syllables (parts of words) that we want to appear in your random word. Next, we generate some random numbers N, from 1 to the number of desired syllables, and we add syllable number N: Syllabs: array[1..8] of string = ('bin', 'cla', 'del', 'for', 'lon', 'pre', 'sta', 'try'); S = ''; for i:= 1 to 3 do begin // word of 3 syllables N := random(8) + 1; S := S + Syllabs[N]; end;
Notes
|
|
Database Tutorials FAQ Crash Course Delphi Tips Source Code Downloads Links
Copyright 1999-2022
DelphiLand