Samuel Newport Samuel Newport - 4 months ago 33
Javascript Question

list fo random numbers separated by commas

Okay so I am making a mario inspired game with randomly generating terrain, It is all working fine however the array of random numbers that randomises the terrain must be the same each time so that the user can enter a seed which is then merged with the larger list to provide a set of random numbers based off of the seed however I cannot think of any way to make this array the same each time without writing it out, and even then making an array of 1000 numbers will be timely. Can anyone suggest a fast way (number generators online dont format it in one single line of numbers separated by numbers so cannot use them)
or could someone provide me with a list that is on a single line separated by numbers that i can easily copy and paste into an array thanks! :)

Answer

The following code in Javascript will generate 1000 random numbers separated by commas.

var string = "";
var numberOfRandomNumbers = 1000;
for (var i = 0; i < numberOfRandomNumbers; i++) {
    var randomNumber = Math.floor((Math.random() * 1000) + 1); //Will generate random number between 1 and 1000
    string += randomNumber+",";
}
console.log(string.substring(0, string.length - 1)); //Print string to console and remove last comma
Comments