Bookstore

Recent

Author Topic: [SOLVED]Is there a function to randomly pick from list of strings?  (Read 343 times)

laznewb

  • New Member
  • *
  • Posts: 20
Hello,

Is there a function to pseudorandomly pick from a list of constant strings like this?

Code: Pascal  [Select]
  1. var
  2.   s1, s2, s3, str: string;
  3. begin
  4.   s1 := 'black';
  5.   s2 := 'white';
  6.   s3 := 'orange';
  7.  
  8.   str := RandomlySelect(s1, s2, s3); {psudocode}
  9. end;

Thanks for any advice  ;)
« Last Edit: February 16, 2020, 09:18:52 pm by laznewb »

Handoko

  • Hero Member
  • *****
  • Posts: 3434
  • My goal: build my own game engine using Lazarus
Re: Is there a function to randomly pick from list of strings?
« Reply #1 on: February 16, 2020, 05:56:57 pm »
Not sure, but I don't think there is such function. You can write your own RandomlySelect, it's not hard. This is my solution:

Code: Pascal  [Select]
  1. function RandomlySelect(const S1, S2, S3: string): string;
  2. begin
  3.   case Random(3) of // <----- change the number to the count of the items
  4.     0: Result := S1;
  5.     1: Result := S2;
  6.     2: Result := S3;
  7.   end;
  8. end;
  9.  
  10. procedure TForm1.Button1Click(Sender: TObject);
  11. var
  12.   s1, s2, s3, str: string;
  13. begin
  14.   s1 := 'black';
  15.   s2 := 'white';
  16.   s3 := 'orange';
  17.   str := RandomlySelect(s1, s2, s3);
  18.   ShowMessage(str);
  19. end;

Bart

  • Hero Member
  • *****
  • Posts: 3720
    • Bart en Mariska's Webstek
Re: Is there a function to randomly pick from list of strings?
« Reply #2 on: February 16, 2020, 05:57:30 pm »
Either put the strings in an array of in a TStringList.
Then use random function to get a pseudorandom index.

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 2579
Re: Is there a function to randomly pick from list of strings?
« Reply #3 on: February 16, 2020, 06:11:58 pm »
An example lifted from one of my programs, but note that you have to set your strings inside a TStringList (or any other TStrings descendant; in my case it's used with a ListBox items):

Code: Pascal  [Select]
  1. {Randomly shuffle the items in List}
  2. procedure Shuffle(const List: TStrings);
  3. var
  4.   Strings: array of String;
  5.   i, Max, Take: Integer;
  6. begin
  7.   if Assigned(List) and (List.Count > 1) then begin
  8.     SetLength(Strings, List.Count);
  9.     Max := List.Count-1 ;
  10.     for i := 0 to Max do begin
  11.       Take := Random(List.Count);
  12.       Strings[i] := List[Take];
  13.       List.Delete(Take);
  14.     end;
  15.     Assert(List.Count = 0, 'Shuffle: List should now be empty but isn''t');
  16.     List.AddStrings(Strings);
  17.   end;
  18. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 1115
Re: Is there a function to randomly pick from list of strings?
« Reply #4 on: February 16, 2020, 06:21:01 pm »
Hi!

Make it simple:

Code: Pascal  [Select]
  1. function RandomlySelect: string;
  2. Const colors: array[0..2] of string = ('black',  'white',  'orange' );
  3. begin
  4. result := colors[random(3) ]
  5. end;
  6.  
Done.

Winni

Handoko

  • Hero Member
  • *****
  • Posts: 3434
  • My goal: build my own game engine using Lazarus
Re: Is there a function to randomly pick from list of strings?
« Reply #5 on: February 16, 2020, 07:30:25 pm »
Code: Pascal  [Select]
  1. function RandomlySelect: string;
  2. Const colors: array[0..2] of string = ('black',  'white',  'orange' );
  3. begin
  4. result := colors[random(3) ]
  5. end;
  6.  
Done.

Winni

I like this one. Easiest and simplest.

lucamar

  • Hero Member
  • *****
  • Posts: 2579
Re: Is there a function to randomly pick from list of strings?
« Reply #6 on: February 16, 2020, 09:03:02 pm »
Code: Pascal  [Select]
  1. function RandomlySelect: string;
  2. Const colors: array[0..2] of string = ('black',  'white',  'orange' );
  3. begin
  4. result := colors[random(3) ]
  5. end;
Done.

Winni

I like this one. Easiest and simplest.

Yes, for a small set of constant strings it's probably the best :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

laznewb

  • New Member
  • *
  • Posts: 20
Re: Is there a function to randomly pick from list of strings?
« Reply #7 on: February 16, 2020, 09:18:39 pm »
Hi!

Make it simple:

Code: Pascal  [Select]
  1. function RandomlySelect: string;
  2. Const colors: array[0..2] of string = ('black',  'white',  'orange' );
  3. begin
  4. result := colors[random(3) ]
  5. end;
  6.  
Done.

Winni

Thanks winni, and everyone for your solutions!