PDA

View Full Version : PickRandomName



Function
01-24-2010, 12:57 AM
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
01-24-2010, 12:58 AM
Should go in Public Test Corner.

Raskolnikov
01-24-2010, 01:01 AM
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
01-24-2010, 01:04 AM
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
01-24-2010, 01:09 AM
function PickRandomName(Arr : TStringArray) : string;
begin
result := Arr[Random(GetArrayLength(Arr))];
end;

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

Function
01-24-2010, 01:13 AM
Wow I'm not a good scripter :/

Sex
01-24-2010, 01:22 AM
Don't worry bro, you'll learn :). It just takes practice.

Raskolnikov
01-24-2010, 01:40 AM
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
01-24-2010, 01:44 AM
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
01-24-2010, 01:47 AM
You all know I won.
function PickRandomName(Names: TStringArray): string; begin Result := Names[Random(Length(Names))]; end;

Raskolnikov
01-24-2010, 01:54 AM
You all know I won.
function PickRandomName(Names: TStringArray): string; begin Result := Names[Random(Length(Names))]; end;

Epic win!

Wanted
01-24-2010, 01:55 AM
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
01-24-2010, 01:56 AM
Lol, you took mine and put it all one 1 line.

Yes, yes I did. :D

Wanted
01-24-2010, 01:59 AM
Yes, yes I did. :D

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

Zyt3x
01-24-2010, 08:26 AM
var Arr : TStringArray;
begin
Arr := ['Brian', 'Brad', 'Levi', 'Adam', 'Mike']
WriteLn(Arr[Random(GetArrayLength(Arr))]);
end.

Sex
01-24-2010, 08:46 AM
Code:

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



That be mine :p.

Zyt3x
01-24-2010, 08:49 AM
That be mine :p.4 lines shorter :f:
And the speed is around 0.000001ms faster :)

Craig`
01-24-2010, 11:12 AM
%w(bob craig dan).choice
# or ruby 1.9.# version..
%w(bob craig dan).sample


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

King of Knives
01-24-2010, 07:44 PM
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!