jeudi 24 août 2023

Java Generating Random Numbers

I am creating a 2d tile map with a method called generateMap.There is an array called map which keeps what tile will be on x and y locations.There are only 4 types of tiles and they are represented in numbers,1-2-3-4.

Numbers also triggers different colors.That's why they are here.

For example : map[10][10]=1 = means there will be a tile number 1 on x10 and y10.(Green)

The thing is all four types of tiles has the same probability to show up,but somehow every time I run the method,"4"becomes much more frequent than the others.

For every tile that doesn't have a neighboring tile,which means every tile with either x0 location or y0 location,I assign them a random number.But with every tile with eight neighboring tiles at least one with same type,has a higher chance to have same type as them.

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.concurrent.ThreadLocalRandom;

public class Map {
public int tilesize=20;
public int tiletype;
public int mapx,mapy=0;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int userwidth = (int) screenSize.getWidth();
int userheight=  (int) screenSize.getHeight();
int width = tilesize/userwidth;
int height = tilesize/userheight;
public int map[][] = new int [140][80]; 

    public void generateMap() {
        
        
        for(int x=0; x<130; x++) {
            for(int y=0; y<70; y++) {
                
                if(x==0 || y==0) {
                    int randomtile=ThreadLocalRandom.current().nextInt(0, 5);
                    map[x][y]=randomtile;   
                }
                else {
                if(map[x][y-1]==1 || map[x][y+1]==1 || map[x-1][y]==1 || map[x+1][y]==1
                || map[x+1][y+1]==1 || map[x-1][y-1]==1 || map[x-1][y+1]==1 || map[x+1][y-1]==1) 
                {
                    int randomtile=ThreadLocalRandom.current().nextInt(0, 10);  
                    if(randomtile>=1 && randomtile <=7) {
                        map[x][y]=1;
                    }
                    else {
                        int randomtile2=ThreadLocalRandom.current().nextInt(0, 5);
                        map[x][y]=randomtile2;
                    }
                }
                
                
                if(map[x][y-1]==2 || map[x][y+1]==2 || map[x-1][y]==2 || map[x+1][y]==2
                || map[x+1][y+1]==2 || map[x-1][y-1]==2 || map[x-1][y+1]==2 || map[x+1][y-1]==2)
                {
                    int randomtile=ThreadLocalRandom.current().nextInt(0, 10);  
                    if(randomtile>=1 && randomtile <=7) {
                        map[x][y]=2;
                    }
                    else {
                        int randomtile2=ThreadLocalRandom.current().nextInt(0, 5);
                        map[x][y]=randomtile2;
                    }
                }
                
                
                if(map[x][y-1]==3 || map[x][y+1]==3 || map[x-1][y]==3 || map[x+1][y]==3
                || map[x+1][y+1]==3 || map[x-1][y-1]==3 || map[x-1][y+1]==3 || map[x+1][y-1]==3) 
                {
                    int randomtile=ThreadLocalRandom.current().nextInt(0, 10);  
                    if(randomtile>=1 && randomtile <=7) {
                        map[x][y]=3;
                    }
                    else {
                        int randomtile2=ThreadLocalRandom.current().nextInt(0, 5);
                        map[x][y]=randomtile2;
                    }
                }
                
                
                if(map[x][y-1]==4 || map[x][y+1]==4 || map[x-1][y]==4 || map[x+1][y]==4
                || map[x+1][y+1]==4 || map[x-1][y-1]==4 || map[x-1][y+1]==4 || map[x+1][y-1]==4) 
                {
                    int randomtile=ThreadLocalRandom.current().nextInt(0, 10);  
                    if(randomtile>=1 && randomtile <=7) {
                        map[x][y]=4;
                    }
                    else {
                        int randomtile2=ThreadLocalRandom.current().nextInt(0, 5);
                        map[x][y]=randomtile2;
                    }
                }
                    
                    
                    
                }
                
                
            }
        }
    }


}

My problem is understanding why 4 is more frequent.




Aucun commentaire:

Enregistrer un commentaire