SRL Archive - NOTA

PickRandomName


Function

2010-01-23 19:57:55

Useful for when you need to decided who gets that last piece of pizza :p
program new;
function PickRandomName( Names: TStringArray ): String;
var
i, rand: integer;
bools: Array of Boolean;

begin
SetArrayLength( bools, High( Names ) );
rand := Random( High( Names ) );
for i := 0 to high( Names ) do
if( i = rand )then
begin
result := Names[ i ];
break;
end;
end;

begin
writeln( PickRandomName( [ 'Brian', 'Brad', 'Levi', 'Adam', 'Mike' ] ) );
end.

Improvements?

Mr. Doctor

2010-01-23 19:58:58

Should go in Public Test Corner.

Raskolnikov

2010-01-23 20:01:14

No, this isn't for SRL, its just a general thing.

And, you could totally shorten this.

Edit: shortened more.

function PickRandomName( Names: TStringArray ): String;
var i, rand: integer;
begin
rand := Random( High( Names ) );
Result := Names[rand];
end;

No need for the "for to do" loop.

Wanted

2010-01-23 20:04:09

From: Cut em2 it
No, this isn't for SRL, its just a general thing.

And, you could totally shorten this.

Edit: shortened more.

function PickRandomName( Names: TStringArray ): String;
var i, rand: integer;
begin
rand := Random( High( Names ) );
Result := Names[rand];
end;

No need for the "for to do" loop.


Actually that High would need to be a Length()…

function PickRandomName(Names: TStringArray): string;
begin
Result := Names[Random(Length(Names))];
end;

Sex

2010-01-23 20:09:19

function PickRandomName(Arr : TStringArray) : string;
begin
result := Arr[Random(GetArrayLength(Arr))];
end;

begin
Writeln(PickRandomName(['Brian', 'Brad', 'Levi', 'Adam', 'Mike']));
end.
:p.

Function

2010-01-23 20:13:57

Wow I'm not a good scripter :/

Sex

2010-01-23 20:22:00

Don't worry bro, you'll learn :). It just takes practice.

Raskolnikov

2010-01-23 20:40:59

Don't get discouraged. It is better if you know how to spell these things out, and what they mean. When you understand the base, (which it seems like you do) then you know why and how to combine them and make it shorter.

Good luck!

Frement

2010-01-23 20:44:12

It's all about perspective. When you see someone shortening it, you realize you could have done it that way too. You are aware of the method, but you cant think of it when you first try to make it.

Timer

2010-01-23 20:47:38

You all know I won.
function PickRandomName(Names: TStringArray): string; begin Result := Names[Random(Length(Names))]; end;

Raskolnikov

2010-01-23 20:54:46

From: Timer
You all know I won.
function PickRandomName(Names: TStringArray): string; begin Result := Names[Random(Length(Names))]; end;


Epic win!

Wanted

2010-01-23 20:55:16

From: Timer
You all know I won.
function PickRandomName(Names: TStringArray): string; begin Result := Names[Random(Length(Names))]; end;


Lol, you took mine and put it all one 1 line.

just for lulz though

library RandomName;

uses
FastShareMem,
SysUtils,
Windows,
Math;

{$R *.res}

function PickRandomName(Names: TStringArray): string; stdcall;
begin
Result := Names[Random(Length(Names))];
end;

function GetFunctionCount(): Integer; stdcall; export;
begin
Result := 1;
end;

function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall;
begin
case x of
0:begin
ProcAddr := @PickRandomName;
StrPCopy(ProcDef, 'function PickRandomName(Names: TStringArray): string; ');
end;
else
x := -1;
end;
Result := x;
end;

exports GetFunctionCount;
exports GetFunctionInfo;

end.

pwnt.

Timer

2010-01-23 20:56:29

From: IceFire908
Lol, you took mine and put it all one 1 line.


Yes, yes I did. :D

Wanted

2010-01-23 20:59:37

From: Timer
Yes, yes I did. :D


Who cares if it's on one line? Mine is 100xs faster. :stirthepot:

Gu3st...

2010-01-24 03:26:01

var Arr : TStringArray;
begin
Arr := ['Brian', 'Brad', 'Levi', 'Adam', 'Mike']
WriteLn(Arr[Random(GetArrayLength(Arr))]);
end.

Sex

2010-01-24 03:46:31

From: Zyt3x
Code:
[LEFT]var Arr : TStringArray;
begin
Arr := ['Brian', 'Brad', 'Levi', 'Adam', 'Mike']
WriteLn(Arr[Random(GetArrayLength(Arr))]);
end.
[/LEFT]


That be mine :p.

Gu3st...

2010-01-24 03:49:51

From: Sex
That be mine :p.
4 lines shorter :f:
And the speed is around 0.000001ms faster :)

Craig`

2010-01-24 06:12:20

[PHP]
%w(bob craig dan).choice
# or ruby 1.9.# version..
%w(bob craig dan).sample
[/PHP]

oh wait, that's not pascal, tehe..

King of Knives

2010-01-24 14:44:58

You're not terrible, you're a beginner. A terrible beginner would be something like this:
funtcion(names:StringTArray;): String
var
lol := eleven
beging
result := names[lol):
end.

In my early days, I did a function that was about 120 lines long. After a bit of training, I boiled it down to 4 lines. 4!