My rendom gen function problem ?

Posted on 2006-05-16
Last Modified: 2010-04-05
I have writtern the folowing function

function RandGen(option : integer; aWordLen : integer) : string;
   aSRC, LettersNumbers, Letters, Numbers : string;

   LettersNumbers := 'abcdefghijklmnoprstuvzyxwqABCDEFGHIJKLMNOPRSTUVZXYQW1234567890';
   Letters := 'abcdefghijklmnoprstuvzyxwqABCDEFGHIJKLMNOPRSTUVZXYQW';
   Numbers := '1234567890';

   if option = 1 then aSRC := LettersNumbers;
   if option = 2 then aSRC := Letters;
   if option = 3 then aSRC := Numbers;

   result := '';

   while Length(result) <= aWordLen do
   result := result + aSRC[Random(Length(aSRC))];


And i'm calling this function from loop which will repeat 20 times like this
var := varP + RandGen(2, Random(5) + 5) + RandGen(3, Random(3));
var2 :=  RandGen(1, Random(4) + 6);

So my program need to genrete for example 20 strings, but when it generetes  1 or 2 or 5 or... i get error. "Index was outside the bounds of the array."

Can you tell me what is the problem and fix the code.

Thank you !
Question by:65zgtre45rr
    LVL 26

    Accepted Solution

    Random returns a number from 0 to Range-1, and if it returns back zero on you, it will error (string index is from 1 to length). So change the following:

    result := result + aSRC[Random(Length(aSRC))+1];



    Assisted Solution

    rllibby is correct.
    but however i made a another way most simple to do the same thing.

    function RandGen(option : integer; aWordLen : integer) : string;
    x : integer;
    result := '';  //clear result
    Randomize; //start with randomized values
    case option of
    1 : begin //Letters and numbers
         for x := 1 to awordlen do
         case random(3) of
         0: result := result+                char(randomrange(ord('a'),ord('z')+1));
         1: result := result+uppercase(char(randomrange(ord('a'),ord('z')+1)));
         2: result := result+                char(randomrange(ord('0'),ord('9')+1));

    2 : begin //Letters
         for x := 1 to awordlen do
         case random(2) of
         0:  result := result+                char(randomrange(ord('a'),ord('z')+1));
         1:  result := result+uppercase(char(randomrange(ord('a'),ord('z')+1)));

    3 : begin //numbers
         for x := 1 to awordlen do
         result := result+char(randomrange(ord('0'),ord('9')+1));
    end; // end of case

    Author Comment

    This is not neded rllibby's solution works.

    Thank you !

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now