Recent

Author Topic: [SOLVED] Random not working  (Read 3159 times)

klyxmaster

  • New Member
  • *
  • Posts: 25
[SOLVED] Random not working
« on: October 25, 2016, 06:58:22 pm »
Been playing around with a simple game, however, I notice that it only generates a random sequence once in awhile". I have not been able to pin point that exactly.
Of late it seems when I do a compile, then the random changes.
So my question is:
Is there anything that I could be doing that would negate the random effect?

...snippit...
Code: Pascal  [Select][+][-]
  1.  
  2. Randomize;
  3.  
  4.     map := thisMap(Player.Qx,Player.Qy); // <- creates a txt file holding the map i.e.: 6x3.txt for quad 6,3
  5.    
  6.     if Not FileExists('maps\' + map) then begin
  7.        
  8.         for i := 1 to maxInns do begin
  9.             if (IQx[i] = Player.Qx) and
  10.                 (IQy[i] = Player.Qy) then begin            
  11.                     repeat
  12.                         x := Random(10)+1;
  13.                         y := Random(10)+1;
  14.                     until isEmptySpot(x,y,True);
  15.                     Sector[x,y] := InnTileId;    
  16.                     break;
  17.             end;
  18.         end;
  19.    
  20.        
  21.        
  22.         for i := 1 to maxInns do begin
  23.             if (CQx[i] = Player.Qx) and
  24.                 (CQy[i] = Player.Qy) then begin            
  25.                     repeat
  26.                         x := Random(10)+1;
  27.                         y := Random(10)+1;
  28.                     until isEmptySpot(x,y,True);
  29.                     Sector[x,y] := CastleTileId;    
  30.                     break;
  31.                
  32.             end;
  33.         end;
  34.  
  35.         for y := 1 to 10 do begin
  36.             for x := 1 to 10 do begin          
  37.                 if Sector[x,y] = 0 then begin
  38.                     i := Random(10);        
  39.                     case i of                      
  40.                         6..10: Sector[x,y] := 0;
  41.                          2..5: Sector[x,y] := 1;
  42.                          else Sector[x,y] := 2;
  43.                     end;                
  44.                 end;
  45.             end;          
  46.         end;
  47.        

this just makes a simple 10x10 ascii forest (if you will) where players wander about. problem is when they exceed this section (sector) the quadrant changes - but it doesnt create a "random" map, just uses the same pattern.

in a nut shell (for those that want to know) this is a very very smple text game converted from an old 80's  game called monster combat. I am rewriting it from scratch with some new features, one of them that remembers the map layout from quadrant to quadrant, and even saves the sector layout as well, so it eliminates the random forest when you come back (not to mention the inns and castles that wander about as well).

But.. I digress, random will not give me a new layout after the first one, but sometimes it does. And I cannot definitively repeat that "sometimes".
« Last Edit: October 25, 2016, 08:15:14 pm by klyxmaster »

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Random not working
« Reply #1 on: October 25, 2016, 07:59:09 pm »
Randomize should be called only once in the program. Is that the case?

Cheers,
VTwin
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

Eugene Loza

  • Hero Member
  • *****
  • Posts: 673
    • My games in Pascal
Re: Random not working
« Reply #2 on: October 25, 2016, 08:06:12 pm »
I think you're missing a clearing of the current "Sector" before creating a new one.
Code: Pascal  [Select][+][-]
  1.   if Not FileExists('maps\' + map) then begin
  2.        
  3.    for x := 1 to 10 do
  4.      for y:=1 to 10 do sector[x,y]:=0; //or whatever value you use for isEmptySpot
  5.  
  6.         for i := 1 to maxInns do begin

because when you ask isEmptySpot(x,y,True); it can't find a free spot, due to only less than 10% of the sector array values are zero from the first  generation.

Quote
Randomize should be called only once in the program.
I don't think that it might influence random repeatability unless randomize is called more frequently than once per 16 milliseconds. Maybe, cryptographic strength of the algorithm... But, actually yes, there is just no need to randomize each time, once is perfectly enough.
It just sets randseed:=GetTickCount in Windows or FPTIME in Linux nothing more.
« Last Edit: October 25, 2016, 08:16:04 pm by Eugene Loza »
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

klyxmaster

  • New Member
  • *
  • Posts: 25
Re: Random not working
« Reply #3 on: October 25, 2016, 08:10:02 pm »
Randomize should be called only once in the program. Is that the case?

Cheers,
VTwin

It was, but then I started adding it to 3 other sections the use random(I am aware that i needs to be called only once, which I had at the beginning of the program,but, when it started failing, I started "forcing" it at other locations).
would this negate the random effect?

klyxmaster

  • New Member
  • *
  • Posts: 25
Re: Random not working
« Reply #4 on: October 25, 2016, 08:14:43 pm »
I think you're missing a clearing of the current "Sector" before creating a new one.
Code: Pascal  [Select][+][-]
  1.   if Not FileExists('maps\' + map) then begin
  2.        
  3.    for x := 1 to 10 do
  4.      for y:=1 to 10 do sector[x,y]:=0; //or whatever value you use for isEmptySpot
  5.  
  6.         for i := 1 to maxInns do begin

because when you ask isEmptySpot(x,y,True); it can't find a free spot, due to only less than 10% of the sector array values are zero from the first  generation.

BINGO!
gawd I hate overlooking the obvious - lol
I just plugged in a quick procedure that can be called when entering a new sector - to wipe it first. that did the trick!
Thanks!

EDIT:
would it be bad if I said I couldn't see the forest for the trees  :P

Eugene Loza

  • Hero Member
  • *****
  • Posts: 673
    • My games in Pascal
Re: [SOLVED] Random not working
« Reply #5 on: October 25, 2016, 08:16:10 pm »
PS! Have you considered NOT saving maps to disk?
They're left there and when you run the program the second time you get the same maps UNLESS you delete ones used for the last game.
You can set randseed:=somenumber.
I.e. if you do so, you'll get equal sequence of random numbers if somenumber is equal.
Therefore you can just assign a randseed for each Qx,Qy and generate the same maps without saving them to disk and overusing the RAM.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: [SOLVED] Random not working
« Reply #6 on: October 25, 2016, 08:52:47 pm »
Code: Pascal  [Select][+][-]
  1. Randomize;
  2.  
  3. map := thisMap(Player.Qx,Player.Qy); // <- creates a txt file holding the map i.e.: 6x3.txt for quad 6,3
  4.  
  5. if Not FileExists('maps\' + map) then
  6. begin  
  7.   for i := 1 to maxInns do
  8.   begin
  9.     if (IQx[i] = Player.Qx) and
  10.       (IQy[i] = Player.Qy) then
  11.     begin            
  12.       repeat
  13.         x := Random(10)+1;
  14.         y := Random(10)+1;
  15.       until isEmptySpot(x,y,True);
  16.       Sector[x,y] := InnTileId;    
  17.       break;
  18.     end;
  19.   end;
  20. end;
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018