- Home /
How to generate random numbers?
I'm creating a random tile-based map generator. I have pre-made sets of integers to make specific rooms or hallways for the generator but I want to be able to randomly select what integer set is made. Here is the level creator function -- any help would be appreciated :)
private void CreateLevel () {
string[] mapData = new string[] {
"03010", "03010", "03010", "22022", "22222"
};
int mapX = mapData[0].ToCharArray().Length;
int mapY = mapData.Length;
Vector3 worldStart = Camera.main.ScreenToWorldPoint (new Vector3 (0, Screen.height));
for (int y = 0; y < mapY; y++) {
char[] newTiles = mapData [y].ToCharArray ();
for (int x = 0; x < mapX; x++) {
PlaceTile (newTiles[x].ToString(), x, y, worldStart);
}
}
}
Answer by $$anonymous$$ · Aug 28, 2017 at 01:15 AM
You can use the Random.Range function so:
int randomNum = Random.Range(0,mapData.Length);
char[] newTiles = mapData [randomNum].ToCharArray ();
note that when using integers in Random.Range the maximum is not inclusive
Your answer
Follow this Question
Related Questions
Lets talk multiplayer: General Questions? 2 Answers
Making a Script that places Objects at random possition but not at the same as a taken position 1 Answer
Save a randomly generated terrain to game file 1 Answer
Find bounds of random generation 0 Answers
Making A hex map, want some random generation, could use ideas. 1 Answer