Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[VB6] Why does this keep creating a pattern?


azkanan
 Share

Recommended Posts

Why does the below keep creating a pattern, example image shown below.

The theory is that this should generate a grid-based map, based on the ID of each point in the array (AlphaSpace.area(0,63)).

ID 1 is a planet (blue), ID 2 is a second planet (red).

"i" generates the columns and "i" generates the rows… So for each passing of **"i"** it should generate 64 (including 0) ID spaces in the rows.

For each ID space, there is a 10% chance of planet 1 and a 10% chance of planet 2, with 80% chance of space… The process seems random, so

why is it making a godamn pattern?

Note: "i" and "i2" are Public variables.

```
For i = 0 To 63

For i2 = 0 To 63

x = Rand(1, 100)

If x > 0 And x < 10 Then

AlphaSpace.area(i, i2).ID = 1

ElseIf x >= 9 And x < 20 Then

AlphaSpace.area(i, i2).ID = 2

End If

Next i2

Next i
```

![](http://puu.sh/2Cund.png)

========================================================

This is my display code, which loads the graphics.

```

For i = 0 To 20

For i2 = 0 To 20

If AlphaSpace.area(i2, i).ID = 0 Then

SpacePic.Picture = LoadPicture(App.Path & "\graphics\spacemap\void.jpg")

ElseIf AlphaSpace.area(i2, i).ID = 1 Then

SpacePic.Picture = LoadPicture(App.Path & "\graphics\spacemap\planet.jpg")

ElseIf AlphaSpace.area(i2, i).ID = 2 Then

SpacePic.Picture = LoadPicture(App.Path & "\graphics\spacemap\planet2.jpg")

End If

'End If

SpacePicture.PaintPicture SpacePic, (i2 * 20), (i * 20)

Next i2

Next i

```
Link to comment
Share on other sites

i'd suggest that it's because you're not using your random function as ridiculously enough.

There is no such thing as truly random generation in code. so there will always be a pattern. if you narrow it so simply "1-10,10-20"

You're gunna get pretty much the same results.

SO, i'd suggest instead of trying to randomly generate per tile in a loop. just use random tiles. something like

for i = 1 to max_objects

randx = 1 to screenwidth

randY = 1 to screen width

tile[x,y]= object.

Works better IMO
Link to comment
Share on other sites

> ' timestamp='1366317482' post='894413']
>
> i'd suggest that it's because you're not using your random function as ridiculously enough.
>
> There is no such thing as truly random generation in code. so there will always be a pattern. if you narrow it so simply "1-10,10-20"
>
> You're gunna get pretty much the same results.
>
> SO, i'd suggest instead of trying to randomly generate per tile in a loop. just use random tiles. something like
>
> for i = 1 to max_objects
>
> randx = 1 to screenwidth
>
> randY = 1 to screen width
>
> tile[x,y]= object.
>
> Works better IMO

```
Public Sub GenerateSpace()

Dim x As Integer

Dim i As Integer

Dim i2 As Integer

Dim emptytile As Integer

emptytile = 1

Do Until emptytile = 0

i = Rand(0, 63)

i2 = Rand(0, 63)

If AlphaSpace.area(i, i2).ID <> 0 Then

i = Rand(1, 63)

i2 = Rand(1, 63)

Else

x = Rand(1, 3)

Select Case x

Case 1: AlphaSpace.area(i, i2).ID = 1

Case 2: AlphaSpace.area(i, i2).ID = 2

Case 3: AlphaSpace.area(i, i2).ID = 3

End Select

End If

emptytile = 0

For i = 0 To 63

For i2 = 0 To 63

If AlphaSpace.area(i, i2).ID = 0 Then

emptytile = emptytile + 1

End If

Next i2

Next i

Loop

End Sub

```

Well, in theory it works, but in practice, the game freezes for a long, long time as it tries to cover all those tiles…

The problem is, there are 4096 tiles, so there's a 1/4096 chance every time it tries of actually filling a tile. Odds are against the computer hitting those last 2 or 3 empty tiles.
Link to comment
Share on other sites

Turns out it's random if it crashes. So, I decided to make my zones smaller (63,63) to (29,29)…

Now, it loads quicker, I can fit it all in one viewscreen without need for scrolling, and I'm ready to move on with my game's development - thanks everyone for the help! ![:)](http://www.touchofdeathforums.com/community/public/style_emoticons/<#EMO_DIR#>/smile.png)

![](http://puu.sh/2CBU7.png)
Link to comment
Share on other sites

Er. Could've just put a seed based on a manipulated calculation of the system time, for example, in your random function, or some other manipulable variable. ![:P](http://www.touchofdeathforums.com/community/public/style_emoticons/<#EMO_DIR#>/tongue.png)

Randomize can have an argument. That argument would be the seed used.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...