Solved

Random!!!

Posted on 2001-06-02
4
222 Views
Last Modified: 2010-04-06
Hello everybody
Actually I just need a procedure that runs randomly  one of the
case...of numbers (look below), but thats not all, the difficult part is that it has to run all numbers, and not a number twice. I hope you understood.

Here an example, the first number is randomly selected between 1 and
10 e.x: 9 now it runs in the case...of:
9: begin {do something} end;  
ok the whole procedure runs totally 10 times, so the next time maybe 2 is selected but not 9! I tried to show the procedure how it should look like:


procedure rand;
var

what:integer;
{your vars}

begin

randomize;
{your code}

case what of

1:     begin
     {do my code}
     end;
2:     begin
     {do my code}
     end;
3:     begin
     {do my code}
     end;
{...}
{the numbers go until 10, each number only once!}
end;

end;

{By the way I tried to make it simple, because in my application it has
to choose from about 70 (!) numbers. I wrote my own code, but it didn't work; because it's quite long I didn't post it.}

:)dluedi
0
Comment
Question by:dluedi
4 Comments
 
LVL 2

Accepted Solution

by:
DidierD earned 30 total points
Comment Utility
dluedi,

You can do it like this


Procedure Rand(List: TStringList);
var
  what, Index: integer;
begin
  Randomize;
  Index := Random(List.Count);
  What := StrToInt(List[Index]);
  List.Delete(Index);
  case what of
    1: ShowMessage('1');
    2: ShowMessage('2');
    3: ShowMessage('3');
    4: ShowMessage('4');
    5: ShowMessage('5');
    6: ShowMessage('6');
    7: ShowMessage('7');
    8: ShowMessage('8');
    9: ShowMessage('9');
    10: ShowMessage('10');
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  NumberList: TStringList;
  i: integer;
begin
  NumberList := TStringList.Create;
  for i := 1 to 10 do
  begin
    NumberList.Add(IntToStr(i));
  end;
  for i := 1 to 10 do
  begin
    Rand(NumberList);
  end;
  NumberList.Free;
end;
0
 
LVL 1

Expert Comment

by:dyancer
Comment Utility
try this url,
I hope it help you.
http://www.delphi3000.com/articles/article_1898.asp

0
 
LVL 1

Expert Comment

by:mpoots
Comment Utility
Hi,

I am not sure but I think you are looking for something like this:


procedure TfrmMain.Button1Click(Sender: TObject);
var i, j : integer; numbers : string;
begin

  randomize;
  numbers := '';
  for j := 0 to 8 do
    while true do begin
      //this gives random numbers between 1 and 10
      i := Random(10 - (1 + 1)) + 1 + 1 ;
      if pos(IntToStr(i),numbers) = 0 then begin
        numbers := numbers + IntToStr(i);
        showmessage(IntToStr(i));
        break;
      end;
    end
end;

Good luck

Marcel    
0
 
LVL 1

Author Comment

by:dluedi
Comment Utility
Actually this was already the answer to my question, that went farst! Thx to everybody else who tried to help!
0

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!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

8 Experts available now in Live!

Get 1:1 Help Now