How to randomly choose one of several strings?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to randomly choose one of several strings?



I have this code:


letsdoit(something,'abcd');
letsdoit(something,'asdfasdf');
letsdoit(something,'gagaga');



I want it to be, if possible, just one simple line with the logic like:


(letsdoit(something,'abcd')) OR (letsdoit(something,'asdfasdf')) OR (letsdoit(something,'gagaga'));



so it has 33% chance to choose either of the three. Or:


(letsdoit(something,'abcd')) OR (letsdoit(something,'asdfasdf'))



so it has 50% chance to choose the left or 50% chance for the right thing.



My questions are:



How to randomize between the three and only do one thing.



How to randomize between two things and only do one.





Call Random(3) and the job is done
– David Heffernan
Aug 12 at 15:08





@DavidHeffernan what is the (3) in your example?
– Fakir Handez
Aug 12 at 15:16





It's the number of choices. I take it you read the documentation for Random?
– David Heffernan
Aug 12 at 15:20





thanks i have solved it like this now var randomer: integer; randomer := Random(3); if randomer = 0 then begin letsdoit(something,'abcd'); end; if randomer = 1 then begin letsdoit(something,'asdfasdf'); end; if randomer = 2 then begin letsdoit(something,'gagaga'); end;
– Fakir Handez
Aug 12 at 16:07


var randomer: integer; randomer := Random(3); if randomer = 0 then begin letsdoit(something,'abcd'); end; if randomer = 1 then begin letsdoit(something,'asdfasdf'); end; if randomer = 2 then begin letsdoit(something,'gagaga'); end;





do not forget to call Randomize() at the start of your application...
– whosrdaddy
Aug 13 at 7:33


Randomize()




1 Answer
1


function RandomChooseString(const A: array of string): string;
begin
Result := A[Random(Length(A))]
end;

letsdoit(something, RandomChooseString(['aaa', 'bbb', 'ccc']));





i have a question, you used the "letsdoit" but where is the "something"? And I get an error: Error: Incompatible types: 'Integer' and 'String'
– Fakir Handez
Aug 12 at 14:48





Just add this parameter of your letsdoit. I've shown a method to choose random string. How to use it - your task.
– MBo
Aug 12 at 14:52



letsdoit





Do you realize that RandomChooseString does?
– MBo
Aug 12 at 15:04


RandomChooseString





I have to think about it now, I hope i will understand soon
– Fakir Handez
Aug 12 at 15:08





I don't understand all the (const A: array of string): string; at the top and get some "Incompatible types: 'Integer' and 'String'" error, but it's probably a good answer. i'm just too stupid to understand my mistake
– Fakir Handez
Aug 12 at 16:11


(const A: array of string): string;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS