[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

My rendom gen function problem ?

Posted on 2006-05-16
3
Medium Priority
?
176 Views
Last Modified: 2010-04-05
I have writtern the folowing function

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

   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))];

end;



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 !
0
Comment
Question by:65zgtre45rr
3 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 1400 total points
ID: 16695540
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];

Regards,
Russell


0
 

Assisted Solution

by:djConex
djConex earned 600 total points
ID: 16696474
rllibby is correct.
but however i made a another way most simple to do the same thing.

function RandGen(option : integer; aWordLen : integer) : string;
var
x : integer;
begin
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));
     end;
    end;

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)));
     end;
    end;

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

Author Comment

by:65zgtre45rr
ID: 16701434
This is not neded rllibby's solution works.

Thank you !
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview
Suggested Courses

872 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