View Full Version : Randomization problems.

28-10-2010, 04:04 PM
Hi I hope some more experienced people can help me. I need a procedure that sets all 100 values of a 10x10 matrix to either 0 or 3(Randomly). Now my solution looked like this:

type matrix100=array[1..10] of array[1..10] of byte;//This is the type for the matrix

Procedure fill_matrix(m:matrix100);

i,j,rand:byte;// I use byte because none of these values should be too big.
for i:=1 to 10 do
for j:=1 to 10 do //double for cycle to set all values of the matrix
if (rand mod 2=1) then m[i][j]:=3 else m[i][j]:=0; {This was an easy way of choosing in setting each value to either 3 or 0}
This however does not give me the wanted result. It sets all values either to just 3 or just 0 and I don't know for sure why because that's why I call randomize inside the for loop. I think it's because as far as I know it uses the system clock for randomization and it's just miliseconds that separates those for loops. Be it this or not I can not find a better way on my own. Can someone help me out?=)

28-10-2010, 04:33 PM
You only need to call randomize() once when your application starts. After that, each number returned by random() should be quite random. :)

Also, you don't have to use 255 and do the mod 2 thing. Random() returns a random value in the range: 0 <= x < param. So just do Random(2) and check if your value is equal to zero. :)

Hope this helps!

28-10-2010, 05:51 PM
Thanks. It works fine now. I changed it to 10 and now it returns 3 if I divide with 9 and mod is 0 or divide with 8 and mod is 1(I didn't want 0 to count for both) else it returns 0. I did this because 50-50% was not the optimal randomization for what I wanted. However I'm proud of myself I solved a problem all by myself by using OOP which I'm still very new to:D Problem was this:

Procedure playerturn;
//do some things and loop until player misses.

Procedure aiturn;
//do some things and loop utnil AI misses

this returned an error because the first procedure couldn't find the second. I found out however if I make an object for both of these procedures I'm able to make the procedures call each other. Cheers:D

28-10-2010, 06:15 PM
You can also fix that by adding:

procedure playerturn;
Procedure aiturn;

in the interface part of your unit (usually just above the word "implementation"). That will make sure that they can always be found by all code in the implementation section. :)

Also, I see that you have two procedures calling eachother. You have to make sure that this won't cause in anfinite loop. These kinds of inifite loops will cause the Stack-overflow error.

28-10-2010, 07:05 PM
Of course I'll make that sure. They will not stack more than a hundred times for sure. That shoudln't cause an error. (As the game field is just 100 squares and after every single square has been shot it is sure that the game has ended). I mostly just use units for procedures and functions that I use often in other programs too (like a weighted mean calculation or a function maximum calculation) and as I progress with this game I have found other use of the "AI" object too but thanks this might be useful later on:)