Results 1 to 6 of 6

Thread: Randomize String

  1. #1

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Randomize String

    VB Code:
    1. Private Sub cmdGenerate_Click()
    2. 'STORE VARIABLES'
    3. Dim sPossible(5) As String, iTemp As Integer
    4. Static iLast As Integer
    5. 'STRINGS TO BE RANDOMIZED'
    6. sPossible(0) = "Hello"
    7. sPossible(1) = "Hello vbForums"
    8. sPossible(2) = "Good day"
    9. sPossible(3) = "Lets go to the pub"
    10. sPossible(4) = "Youre getting married?!"
    11. sPossible(5) = "Im getting a new car today!"
    12. 'RANDOMIZE IT'
    13. Randomize Timer
    14. Do
    15. iTemp = Rnd() * 5
    16. Loop Until iLast <> iTemp
    17. iLast = iTemp
    18. txtMain.Text = sPossible(iTemp)
    19. 'FINISH THE JOB'
    20. txtMain.SetFocus
    21. txtMain.Refresh
    22. End Sub

  2. #2

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    IN DELPHI...

    {$R *.dfm}

    var
    iLast: integer;

    Code:
    procedure TfrmTipOfTheDay.FormCreate(Sender: TObject);
    Var
      sPossible: array[0..5] of string;
      iTemp: integer;
    begin
      frmMain.tmrMain.Enabled:= False;
      //Possible tips
      sPossible[0]:= 'Hello';
      sPossible[1]:= 'Hello vbForums!';
      sPossible[2]:= 'Good day';
      sPossible[3]:= 'Lets go to the pub';
      sPossible[4]:= 'Youre getting married?!';
      sPossible[5]:= 'Im getting a new car today!';
      //Generate random tip
      Randomize;
      repeat
       iTemp:= Random(6);
      Until iLast <> iTemp;
      iLast:= iTemp;
      txtMain.Text:= sPossible[iTemp]
    end;
    Last edited by Madboy; Jun 15th, 2004 at 08:52 AM.

  3. #3

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    You could also put it in a private procedure and call it as needed, useful for say if you have a Next tip button. Although this is in Delphi, you can see how to convert back to VB from above:

    Code:
    var
      iLast: integer;
    
    procedure TfrmTipOfTheDay.RandomizeTips;
    var
      sPossible: array[0..5] of string;
      iTemp: integer;
    begin
      //Possible tips
      sPossible[0]:= 'String 0';
      sPossible[1]:= 'String 1';
      sPossible[2]:= 'String 2';
      sPossible[3]:= 'String 3';
      sPossible[4]:= 'String 4';
      sPossible[5]:= 'String 5';
      //Randomize tips
      Randomize;
      repeat
       iTemp:= Random(6);
      Until iLast <> iTemp;
      iLast:= iTemp;
      txtMain.Text:= sPossible[iTemp]
    end;
    
    procedure TfrmTipOfTheDay.FormCreate(Sender: TObject);
    begin
      RandomizeTips;
    end;
    
    procedure TfrmTipOfTheDay.cmdNextClick(Sender: TObject);
    begin
      RandomizeTips;
    end;
    Only problem is the next tip will never be unique, meaning you could randomize the next tip and get the same tip as the previous one. There are many ways to prevent this however.

  4. #4
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: IN DELPHI...

    Is there any specific reason you are posting delphi code in a VB6 codebank? Can you read ????


  5. #5

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Re: Randomize String

    I said it can be illistrutated for conversion back to VB6

  6. #6

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Re: Randomize String

    Actually it is VB ready, just shows Delphi code too

    Sorry for the mix up

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width