list fo random numbers separated by commas - javascript

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! :)

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

Related

Creating random number on fractional part between two numbers

I am working on the following code. How can I create random number between 150.570 and 150.720?
As you can see the integer-part (150) is always fixed and I just need to get random on fractional-part (between .570 to .720) only.
var gapLeft = Math.floor(Math.random() * 150.720) + 150.570 ;
console.log(gapLeft);
Here is your solution for this,
console.log(150.57+Math.random()*(0.72-0.57));

Random number seeded with custom time

I am trying to create a random number as below using javascript
function valid(form) {
var input = 0;
var input = document.getElementById('custom1').value;
var final_input = input.charAt(0);
var number = 1000000000 - Math.floor(Math.random() * 1000000000);
final_input = final_input + number;
document.getElementById('custom4').value = final_input;
}
The idea is that it will get the value from "custom1" (which is one of the input fields) and then will get the first character. After that it will add next 9 random digits and puts the final value to custom4 (another input field) of the form. The javascript is working fine so far. However, I will rather have the random digit numbers be seeded with current time. I think that will it will be really random. Is that possible?
The JavaScript standard random API doesn't support explicit seeding (which is a shame). Here's the spec :
Returns a Number value with positive sign, greater than or equal to 0
but less than 1, chosen randomly or pseudo randomly with approximately
uniform distribution over that range, using an
implementation-dependent algorithm or strategy. This function takes no
arguments.
If you really need to seed your generator with a given number, you'll have to use a library like this one (not tested by me).
But the JavaScript random API is implicitly seeded, which ensures you'll have different results. There's no ECMAScript specification regarding how it's seeded but it's probable that all browsers use the time for the seeding. The MDN says
The random number generator is seeded from the current time, as in
Java.
I must correct myself. This is only correct for Mozilla (as far as i know): The JavaScript random API uses already the current time as a seed. No need to do it double (and it's not supported).
The specification doesn't mention a algorithm or strategies how the random number is generated and if and how it is seeded.
Thanks..
finally I have something like below
function valid(form) {
var input = 0;
var input = document.getElementById('custom1').value;
var final_input = input.charAt(0);
var number = new Date().valueOf(); /*1000000000 - Math.floor(Math.random() * 1000000000);*/
final_input = final_input + number;
document.getElementById('custom4').value = final_input;
}

Regex for extracting separate letters in a loop with javascript

I'm working on a script to create metrics for online author identification. One of the things I came across in the literature is to count the frequency of each letter (how many a's, how many b's, etc) independent of upper or lower case. Since I don't want to create a separate statement for each letter, I'm trying to loop the thing, but I can't figure it out. The best I have been able to come up with is converting the ASCII letter code in to hex, and then...hopefully a miracle happens.
So far, I've got
element = id.toLowerCase();
var hex = 0;
for (k=97; k<122; k++){
hex = k.toString(16); //gets me to hex
letter = element.replace(/[^\hex]/g, "")//remove everything but the current letter I'm looking for
return letter.length // the length of the resulting string is how many times the ltter came up
}
but of course, when I do that, it interprets hex as the letters h e x, not the hex code for the letter I want.
Not sure why you'd want to convert to hex, but you could loop through the string's characters and keep track of how many times each one has appeared with an object used as a hash:
var element = id.toLowerCase();
var keys = {};
for(var i = 0, len = element.length; i<len; i++) {
if(keys[element.charAt(i)]) keys[element.charAt(i)]++;
else keys[element.charAt(i)] = 1;
}
You could use an array to do the same thing but a hash is faster.

Chunk a string every odd and even position

I know nothing about javascript.
Assuming the string "3005600008000", I need to find a way to multiply all the digits in the odd numbered positions by 2 and the digits in the even numbered positions by 1.
This pseudo code I wrote outputs (I think) TRUE for the odd numbers (i.e. "0"),
var camid;
var LN= camid.length;
var mychar = camid.charAt(LN%2);
var arr = new Array(camid);
for(var i=0; i<arr.length; i++) {
var value = arr[i]%2;
Alert(i =" "+value);
}
I am not sure this is right: I don't believe it's chunking/splitting the string at odd (And later even) positions.
How do I that? Can you please provide some hints?
/=================================================/
My goal is to implement in a web page a validation routine for a smartcard id number.
The logic I am trying to implement is as follows:
· 1) Starting from the left, multiply all the digits in the odd numbered positions by 2 and the digits in the even numbered positions by 1.
· 2) If the result of a multiplication of a single digit by 2 results in a two-digit number (say "7 x 2 = 14"), add the digits of the result together to produce a new single-digit result ("1+4=5").
· 3) Add all single-digit results together.
· 4) The check digit is the amount you must add to this result in order to reach the next highest multiple of ten. For instance, if the sum in step #3 is 22, to reach the next highest multiple of 10 (which is 30) you must add 8 to 22. Thus the check digit is 8.
That is the whole idea. Google searches on smartcard id validation returned nothing and I am beginning to think this is overkill to do this in Javascript...
Any input welcome.
var theArray = camid.split(''); // create an array entry for each digit in camid
var size = theArray.length, i, eachValue;
for(i = 0; i < size; i++) { // iterate over each digit
eachValue = parseInt(theArray[i], 10); // test each string digit for an integer
if(!isNaN(eachValue)) {
alert((eachValue % 2) ? eachValue * 2 : eachValue); // if mod outputs 1 / true (due to odd number) multiply the value by 2. If mod outputs 0 / false output value
}
}
I discovered that what I am trying to do is called a Luhn validation.
I found an algorithm right here.
http://sites.google.com/site/abapexamples/javascript/luhn-validation
Thanks for taking the time to help me out. Much appreciated.
It looks like you might be building to a Luhn validation. If so, notice that you need to count odd/even from the RIGHT not the left of the string.

Counting rounds in a tournament

I've written a huge page in JavaScript for a tournament I'm hosting on a game. I've gotten everything I really need worked out into arrays, but I want to add rounds. The whole script adjusts to tournament settings (for more in the future) and I'd like this to adjust itself as well. So, let's say the tournament settings are [game,teamsize,entrylimit]. The entrylimit will be the key to finding the solution, because that decides the rounds. It works in a tree system (or however it's called). Let's say the entrylimit is 8. That means the first round will consist of 4 matches, and the second will consist of 2. If the entrylimit were 16, then the first round would consist of 8 matches, the second would consist of 4, and the third would consist of 2. I want to find a way to stick this into my loop where matches are written, and use the entrylimit and match number to generate the round number. All I need is a formula that can use those two variables to get my desired result. Also I apologize for the excessive amount of detail.
If I understand the problem, here's an example of how the entrylimit can get the number of rounds and the number of matches in each round.
Calculations:
var entrylimit=16;
var amount_of_rounds = Math.log(entrylimit) / Math.log(2);
for(i=amount_of_rounds; i>0; i--)
{
s = 'Round '+(amount_of_rounds-i+1)+' of '+amount_of_rounds+' consist of '+Math.pow(2, i-1)+' matches';
alert(s);
}
​
Try this:
var entryInfo = [];
function populateEntryInfo(entryLimit)
{
entryInfo = [];
var i = entryLimit;
while(i>1)
{
i = i/2;
entryInfo.push(i);
}
entryInfo.push(i);
}

Categories

Resources