# Improve randomization function (from 1 to 2)

I am trying to do a randomize function. It randomly selects one person (from two) to insert something into the console. The second person goes next, and then they keep alternating until the loop ends.

  randomize();
RandomTurn := random(2)+1;
while (1) do
begin
if RandomTurn = 1 then
PersonTurn((NumberOfMoves mod 2)+1)
else
PersonTurn(round(2/((NumberOfMoves mod 2)+1)));

NumberOfMoves := NumberOfMoves + 1;
end;


What do you think? How could this function be improved based on number of moves/user inputs?

• What's the argument to PersonTurn() and why do you need it? – Thijs van Dien Jul 7 '13 at 9:32
• There are two persons who are changing in their turn like in chess or any other game, so I wanted to do a fnction which will always turn another person BUT at the begining it chooses randomly on of them ... – Byakugan Jul 24 '13 at 20:19

### Randomness

This answer suggests that Random(2) would be okay for most uses. If you want more randomness, you could for example use an implementation of Mersenne Twister. This page lists a few options for Pascal/Delphi, for example this implementation.

### Alternating values

Now that I know what you are looking for, I would write the code like this:

// If Value is divisible by 2 then return 1, otherwise return 2
procedure AlternateValue(Value) : ShortInt;
begin
if Value mod 2 = 0 then Result := 1 else Result := 2
end;

randomize();
RandomTurn := random(2)+1;
NumberOfMoves := RandomTurn

while (1) do
begin
PersonTurn(AlternateValue(NumberOfMoves))
NumberOfMoves := NumberOfMoves + 1;
end;


You should swap the two lines in the while loop if you want to start with the same number as Random gives. From what I gather, it should not matter much, however.

(I have never written any Pascal or Delphi before, so the code and/or syntax may not be perfect or correct.)

• Yes I need to randomize first input to be randomly 1 or 2 and after that changing 1 - 2 - 1 - 2 etc. So only first number will be random and then others will always change, does this code of yours do that? – Byakugan Jul 4 '13 at 11:51
• @Byakugan: To switch between 1 and 2, you could just do NewValue := 3 - OldValue: 3 - 1 -> 2, 3 - 2 -> 1. – Andriy M Jul 4 '13 at 17:29
• Yes, the code I posted would do that. I have revised my answer, however, to indicate how I would write it now that I understand what you are after. You could also do as @AndriyM suggests, but I think my example is cleaner and more readable, because it states exactly what the intent of the code is. Does this answer your question? – Lstor Jul 4 '13 at 19:21
• My snippet had a bug, because I forgot to do mod 2. That is fixed now. – Lstor Jul 5 '13 at 10:22
• @AndriyM Or Result := Value mod 2 + 1;. It would be cleaner to use 0 and 1 rather than 1 and 2... – Thijs van Dien Jul 7 '13 at 9:29

I would improve the readability a bit by using:

PersonToPlay := RandomInitial();
For i:=MoveIndex := 1 to TotalNumberOfMoves do begin
PersonTurn(PersonToPlay);
PersonToPlay := 3 - PersonToPlay;
end;


To make it even more readable:

PersonToPlay := RandomInitial();
For i:=MoveIndex := 1 to TotalNumberOfMoves do begin
PersonTurn(PersonToPlay);
PersonToPlay := TheOtherPlayer(PersonToPlay);
end;
....

Function TheOtherPlayer (aPlayer : TPlayer) : TPlayer;
Begin
if aPlayer := ThisPlayer then
Result := ThatPlayer
else
Result := ThisPlayer;
End;