Given any number between 0 and 1, such as 0.84729347293923, is there a simple way to make it into 84729347293923 without string or regex manipulation? I can think of using a loop, which probably is no worse than using a string because it is O(n) with n being the number of digits. But is there a better way?
function getRandom() {
let r = Math.random();
while (Math.floor(r) !== r) r *= 10;
return r;
}
for (let i = 0; i < 10; i++)
console.log(getRandom());
Integers mod 1 = 0, non integers mod 1 != 0.
while ((r*=10) % 1);
Ok, just want to refactor my code (i realized that was bad so this is what i discovered to correctly get the value as you requested).
NOTE: As the question says that "given any number between 0 and 1", this solution only works for values between 0 and 1:
window.onload = ()=>{
function getLen(num){
let currentNumb = num;
let integratedArray = [];
let realLen = 0;
/*While the number is not an integer, we will multiply the copy of the original
*value by ten, and when the loop detects that the number is already an integer
*the while simply breaks, in this process we are storing each transformations
*of the number in an array called integratedArray*/
while(!(Number.isInteger(currentNumb))){
currentNumb *= 10;
integratedArray.push(currentNumb);
}
/*We iterate over the array and compare each value of the array with an operation
*in which the resultant value should be exactly the same as the actual item of the
*array, in the case that both are equal we assign the var realLen to i, and
*in case that the values were not the same, we simply breaks the loop, if the
*values are not the same, this indicates that we found the "trash numbers", so
*we simply skip them.*/
for(let i = 0; i < integratedArray.length; i++){
if(Math.floor(integratedArray[i]) === Math.floor(num * Math.pow(10, i + 1))){
realLen = i;
}else{
break;
}
}
return realLen;
}
//Get the float value of a number between 0 and 1 as an integer.
function getShiftedNumber(num){
//First we need the length to get the float part of the number as an integer
const len = getLen(num);
/*Once we have the length of the number we simply multiply the number by
*(10) ^ numberLength, this eliminates the comma (,), or point (.), and
*automatically transforms the number to an integer in this case a large integer*/
return num * (Math.pow(10, len));
}
console.log(getShiftedNumber(0.84729347293923));
}
So the explanation is the next:
Because we want to convert this number without using any string, regex or any another thing, first we need to get the length of the number, this is a bit hard to do without using string conversions... so i did the function getLen for this purpose.
In the function getLen, we have 3 variables:
currentNumb: This var is a copy of the original value (the original number), this value help us to found the length of the number and we can do some transforms to this value whitout changing the original reference of the number.
We need to multiply this value any times is needed to transform the number to an integer and then multiplyng this value by ten to ten.
with the help of a while (this method makes the number a false integer).
NOTE: I saw "False integer" because when i was making the tests i realized that in the number is being adding more digits than normal... (Very very strange), so this stupid but important thing makes neccesary the filter of these "trash numbers", so later we proccess them.
integratedArray: This array stores the values of the result of the first while operations, so the last number stored in this array is an integer, but this number is one of the "fake integers", so with this array we need to iterate later to compare what of those stored values are different to the original value multiplied by (10 * i + 1), so here is the hint:
In this case the first 12 values of this array are exactly the same with the operation of Math.floor(num * Math.pow(10, i + 1))), but in the 13th value of the array these values are not the same so... yes!, there are those "trash numbers" that we were searching for.
realLen: This is the variable where we will store the real length of the number converting the float part of this number in an integer.
Some binary search approach:
Its useless if avarage length < 8;
It contains floating point issues.
But hey it is O(log n) with tons of wasted side computations - i guess if one counts them its event worse than just plain multiplication.
I prefer #chiliNUT answer. One line stamp.
function floatToIntBinarySearch(number){
const max_safe_int_length = 16;
const powers = [
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000
]
let currentLength = 16
let step = 16
let _number = number * powers[currentLength]
while(_number % 1 != 0 || (_number % 10 | 0) == 0){
step /= 2
if( (_number % 10 | 0) == 0 && !(_number % 1 != 0)){
currentLength = currentLength - step;
} else {
currentLength = step + currentLength;
}
if(currentLength < 1 || currentLength > max_safe_int_length * 2) throw Error("length is weird: " + currentLength)
_number = number * powers[currentLength]
console.log(currentLength, _number)
if(Number.isNaN(_number)) throw Error("isNaN: " + ((number + "").length - 2) + " maybe greater than 16?")
}
return number * powers[currentLength]
}
let randomPower = 10 ** (Math.random() * 10 | 0)
let test = (Math.random() * randomPower | 0) / randomPower
console.log(test)
console.log(floatToIntBinarySearch(test))
i have this code: the idea is to print the list of the 10 random numbers and then add 6 to each item on the list and finally print new list with the new numbers after the additions: I've tried several modification but all of them are failing, any ideas??
var myArray = [];
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
console.log("Before: " + myArray);
for (var i = 0; i < myArray.lenght; i++) {
myArray = [i] + 5;
}
console.log("After: " + myArray);
one solution can be
let myArray = []
for (let i = 0 ; i < 5 ; i++) {
myArray.push(Math.floor(Math.random() * 10) + 5);
}
console.log(myArray)
A couple of things to help you along your way...
It looks like you're using borrowed code somewhere, because randomNumber is not a native JavaScript function.
So, first to get a random number, you can read more about Math.random() here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
Math.random() natively provides a decimal number between 0 and 1 (0.4, 0.67, 0.321 ...) To get a range, you multiply your answer by the maximum number for the range. And then commonly you want to wrap that in a Math.floor() method to trim the decimal points (this is just a Math method that all it does is trim off anything that's a decimal point and provides a whole number)
W3C is a good place to start to read up about arrays:
https://www.w3schools.com/js/js_arrays.asp
The common convention for adding elements to an array is to use .push() but you can look into .pop() and .filter() and .map() and there's just a ton of helpful methods attached to JavaScript arrays :)
/*
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
*/
let originalArray = [];
let plusSixArray = [];
let max = 10; // set the range for your random number
// max = 10 will let the range be from 0 - 9
// max = 273 will let the range be from 0 - 272
for( var i=0; i<10; i++ ){
let number = Math.floor(Math.random() * max) + 1;
// adding 1 to the end gives you a range between 1 - 10
originalArray.push( number );
// since you're already in a loop, just add the 6
plusSixArray.push( number + 6 );
}
console.log( originalArray );
console.log( plusSixArray );
Can you please let me know how I can create Random 1 or 3 (not Range, only 1 or 3) on each load of document? I already tried this but it returns String instead of numbers.Should I have to parse them to numbers or is there a better way to do this?
var num = [];
var chances = "13";
for (var i = 0; i <1; i++) {
num.push(chances.charAt(Math.floor(Math.random() * chances.length)));
}
console.log(num);
Since Math.random returns a number between 0 (inclusive) and 1 (exclusive):
var value = Math.random() < 0.5 ? 1 : 3;
That said, note that your chances string doesn't have a character at index 3, it's only two characters long, and their indexes are 0 and 1. So if you really want to randomly pick from those two characters, as your code seems to suggest, what you really want is:
var value = Math.random() < 0.5 ? 0 : 1;
or
var value = Math.floor(Math.random() * 2);
...although your code using chances.length is just fine (since chances.length is 2).
Also note that for (var i = 0; i < 1; i++) will only run exactly once, making the loop somewhat pointless. :-)
I'm trying to understand the code example that's at the end of my post and I need help. So this is supposed to be a "lottery". So here's what I see:
for( i = 1 ; i < 50 ; i++ ){ nums[ i ] = i ; }
Here it assigns the value of i (which will be between 1 and 49) to each each i-th element of the nums array. So I guess it's just making an array that contains the numbers 1 to 49. I don't see the point of this whole line, actually, given the code that follows.
for( i = 1 ; i < 50 ; i++ )
{
rand = Math.ceil( Math.random() * 49 ) ;
temp = nums[ i ] ;
nums[ i ] = nums[ rand ] ;
nums[ rand ] =temp ;
}
Here I get confused. So, once again it runs a standard loop from 1 to 49, and for each of the 49 iterations, it:
assigns a random number between 1 and 49 to "rand"
assigns the i-th value of the 'nums' list (which will still be just i, right?) to temp (why?)
assigns the random number to the i-th element of the "nums" array, so presumably if it's at i=1, nums[i] = 1, and now instead of 1 it's the random number.
Then it assigns temp (which was the original value, which was i anyway) back to the value that's in the rand-th position of the nums array.
So what am I misunderstanding here? I don't get the point of this. Where is this leading? Am I reading it correctly? Presumably at the end of this, the numbers inside the array are jumbled up, and then at the end it just picks the first six, which are 6 "random" numbers. But why not just do something like:
var nums=[];
for(i=0; i<6; i++) {
nums[i] == Math.ceil(Math.random() * 49);
}
along with a test to ensure the random numbers weren't the same?
Here's the code example in whole:
function init()
{
var panel = document.getElementById( "panel" ) ;
var i , rand , temp , str , nums = [] ;
for( i = 1 ; i < 50 ; i++ ){ nums[ i ] = i ; }
for( i = 1 ; i < 50 ; i++ )
{
rand = Math.ceil( Math.random() * 49 ) ;
temp = nums[ i ] ;
nums[ i ] = nums[ rand ] ;
nums[ rand ] =temp ;
}
str = "Your Six Lucky Numbers:<br>" ;
for( i = 1 ; i < 7 ; i++ )
{
str += nums[ i ] ;
if( i !== 6 ) { str += " - " ; }
}
panel.innerHTML = str;
}
document.addEventListener( "DOMContentLoaded" , init , false ) ;
The code is supposed to create an array of numbers and then shuffle them. It's almost using a Fisher-Yates shuffle, but not quite. There is also a bug in the random number generation.
Putting the numbers 1 through 49 in the array is not pointless, those are the numbers that are used later on. They are not replaced, they are just moved around in the array.
The next loop will shuffle the items in the array, but first let's look at the random number generation. This way of creating a random number is wrong:
rand = Math.ceil(Math.random() * 49); // Don't use this
The Math.random() method returns a value that is 0 <= x < 1. Multiplying that number by 49 and then using Math.ceil doesn't produce numbers between 1 and 49, it produces numbers between 0 and 49. The number 0 comes very rarely, but you can't ignore it. The correct way to produce the random number is:
rand = Math.floor(Math.random() * 49) + 1;
The code for shuffling the items in the array loops through the items and picks an item by random to swap each item with. This produces a biased shuffle, and it should use the Fisher-Yates algorithm instead:
for (i = 49 ; i >= 1 ; i--) {
rand = Math.floor(Math.random() * i) + 1;
temp = nums[i];
nums[i] = nums[rand];
nums[rand] = temp;
}
The last three lines in the loop swaps two items in the array. It stores away the value from one item, copies the value from the other item into the first, and then puts the stored value in the other item.
(The values of i and rand can actually be the same for some items, so that an item is swapped with itself. This is how the algorithm is supposed to work, it ensures that the item has the same chance of staying in the same place as the chance of being moved to any other place.)
But why not just do something like [code] along with a test to ensure
the random numbers weren't the same?
That is another way of creating the random numbers. It would be somewhat more efficient, at least most of the time (it could theoretically loop forever), but it's about the same amount of code.
Another solution would be to put all the numbers in one array, then pick numbers by random from it and move them to another array. That is the same principle as the shuffling algorithm, but it only needs to shuffle enough to get the first six items in the right place (or the last six items as it would be using the Fisher-Yates algorithm). However, removing items from an array is generally an expensive operation, as the items after it needs to be shifted into its place.
It is indeed a basic shuffle. The point is to ensure we only have unique numbers to begin with.
Doing this shuffle iterates once over the loop.
Checking uniqueness requires iterating once over the loop... for every element in the loop.
For small loops like this, the difference is basically nil, but in general when given two options you should take the one that's less computationally complicated... but always remember to comment the code to explain it to future developers!
I'm trying to generate a random number that must have a fixed length of exactly 6 digits.
I don't know if JavaScript has given below would ever create a number less than 6 digits?
Math.floor((Math.random()*1000000)+1);
I found this question and answer on StackOverflow here. But, it's unclear.
EDIT: I ran the above code a bunch of times, and Yes, it frequently creates numbers less than 6 digits. Is there a quick/fast way to make sure it's always exactly 6 digits?
console.log(Math.floor(100000 + Math.random() * 900000));
Will always create a number of 6 digits and it ensures the first digit will never be 0. The code in your question will create a number of less than 6 digits.
Only fully reliable answer that offers full randomness, without loss. The other ones prior to this answer all looses out depending on how many characters you want. The more you want, the more they lose randomness.
They achieve it by limiting the amount of numbers possible preceding the fixed length.
So for instance, a random number of fixed length 2 would be 10 - 99. For 3, 100 - 999. For 4, 1000 - 9999. For 5 10000 - 99999 and so on. As can be seen by the pattern, it suggests 10% loss of randomness because numbers prior to that are not possible. Why?
For really large numbers ( 18, 24, 48 ) 10% is still a lot of numbers to loose out on.
function generate(n) {
var add = 1, max = 12 - add; // 12 is the min safe number Math.random() can generate without it starting to pad the end with zeros.
if ( n > max ) {
return generate(max) + generate(n - max);
}
max = Math.pow(10, n+add);
var min = max/10; // Math.pow(10, n) basically
var number = Math.floor( Math.random() * (max - min + 1) ) + min;
return ("" + number).substring(add);
}
The generator allows for ~infinite length without lossy precision and with minimal performance cost.
Example:
generate(2)
"03"
generate(2)
"72"
generate(2)
"20"
generate(3)
"301"
generate(3)
"436"
generate(3)
"015"
As you can see, even the zero are included initially which is an additional 10% loss just that, besides the fact that numbers prior to 10^n are not possible.
That is now a total of 20%.
Also, the other options have an upper limit on how many characters you can actually generate.
Example with cost:
var start = new Date(); var num = generate(1000); console.log('Time: ', new Date() - start, 'ms for', num)
Logs:
Time: 0 ms for 7884381040581542028523049580942716270617684062141718855897876833390671831652069714762698108211737288889182869856548142946579393971303478191296939612816492205372814129483213770914444439430297923875275475120712223308258993696422444618241506074080831777597175223850085606310877065533844577763231043780302367695330451000357920496047212646138908106805663879875404784849990477942580056343258756712280958474020627842245866908290819748829427029211991533809630060693336825924167793796369987750553539230834216505824880709596544701685608502486365633618424746636614437646240783649056696052311741095247677377387232206206230001648953246132624571185908487227730250573902216708727944082363775298758556612347564746106354407311558683595834088577220946790036272364740219788470832285646664462382109714500242379237782088931632873392735450875490295512846026376692233811845787949465417190308589695423418373731970944293954443996348633968914665773009376928939207861596826457540403314327582156399232931348229798533882278769760
More hardcore:
generate(100000).length === 100000 -> true
I would go with this solution:
Math.floor(Math.random() * 899999 + 100000)
More generally, generating a random integer with fixed length can be done using Math.pow:
var randomFixedInteger = function (length) {
return Math.floor(Math.pow(10, length-1) + Math.random() * (Math.pow(10, length) - Math.pow(10, length-1) - 1));
}
To answer the question: randomFixedInteger(6);
You can use the below code to generate a random number that will always be 6 digits:
Math.random().toString().substr(2, 6)
Hope this works for everyone :)
Briefly how this works is Math.random() generates a random number between 0 and 1 which we convert to a string and using .toString() and take a 6 digit sample from said string using .substr() with the parameters 2, 6 to start the sample from the 2nd char and continue it for 6 characters.
This can be used for any length number.
If you want to do more reading on this here are some links to the docs to save you some googling:
Math.random(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
.toString(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
.substr(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
short with arbitrary precision
below code ALWAYS generate string with n digits - solution in snippet use it
[...Array(n)].map(_=>Math.random()*10|0).join``
let gen = n=> [...Array(n)].map(_=>Math.random()*10|0).join``
// TEST: generate 6 digit number
// first number can't be zero - so we generate it separatley
let sixDigitStr = (1+Math.random()*9|0) + gen(5)
console.log( +(sixDigitStr) ) // + convert to num
100000 + Math.floor(Math.random() * 900000);
will give a number from 100000 to 999999 (inclusive).
Based on link you've provided, right answer should be
Math.floor(Math.random()*899999+100000);
Math.random() returns float between 0 and 1, so minimum number will be 100000, max - 999999. Exactly 6 digits, as you wanted :)
Here is my function I use. n - string length you want to generate
function generateRandomNumber(n) {
return Math.floor(Math.random() * (9 * Math.pow(10, n - 1))) + Math.pow(10, n - 1);
}
This is another random number generator that i use often, it also prevent the first digit from been zero(0)
function randomNumber(length) {
var text = "";
var possible = "123456789";
for (var i = 0; i < length; i++) {
var sup = Math.floor(Math.random() * possible.length);
text += i > 0 && sup == i ? "0" : possible.charAt(sup);
}
return Number(text);
}
let length = 6;
("0".repeat(length) + Math.floor(Math.random() * 10 ** length)).slice(-length);
Math.random() - Returns floating point number between 0 - 1
10 ** length - Multiply it by the length so we can get 1 - 6 length numbers with decimals
Math.floor() - Returns above number to integer(Largest integer to the given number).
What if we get less than 6 digits number?
That's why you have to append 0s with it.
"0".repeat() repeats the given string which is 0
So we may get more than 6 digits right?
That's why we have to use "".slice() method. It returns the array within given indexes. By giving minus values, it counts from the last element.
I created the below function to generate random number of fix length:
function getRandomNum(length) {
var randomNum =
(Math.pow(10,length).toString().slice(length-1) +
Math.floor((Math.random()*Math.pow(10,length))+1).toString()).slice(-length);
return randomNum;
}
This will basically add 0's at the beginning to make the length of the number as required.
npm install --save randomatic
var randomize = require('randomatic');
randomize(pattern, length, options);
Example:
To generate a 10-character randomized string using all available characters:
randomize('*', 10);
//=> 'x2_^-5_T[$'
randomize('Aa0!', 10);
//=> 'LV3u~BSGhw'
a: Lowercase alpha characters (abcdefghijklmnopqrstuvwxyz'
A: Uppercase alpha characters (ABCDEFGHIJKLMNOPQRSTUVWXYZ')
0: Numeric characters (0123456789')
!: Special characters (~!##$%^&()_+-={}[];\',.)
*: All characters (all of the above combined)
?: Custom characters (pass a string of custom characters to the options)
NPM repo
I use randojs to make the randomness simpler and more readable. you can pick a random int between 100000 and 999999 like this with randojs:
console.log(rando(100000, 999999));
<script src="https://randojs.com/1.0.0.js"></script>
const generate = n => String(Math.ceil(Math.random() * 10**n)).padStart(n, '0')
// n being the length of the random number.
Use a parseInt() or Number() on the result if you want an integer.
If you don't want the first integer to be a 0 then you could use padEnd() instead of padStart().
I was thinking about the same today and then go with the solution.
var generateOTP = function(otpLength=6) {
let baseNumber = Math.pow(10, otpLength -1 );
let number = Math.floor(Math.random()*baseNumber);
/*
Check if number have 0 as first digit
*/
if (number < baseNumber) {
number += baseNumber;
}
return number;
};
Let me know if it has any bug. Thanks.
"To Generate Random Number Using JS"
console.log(
Math.floor(Math.random() * 1000000)
);
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p id="demo"></p>
</body>
</html>
You can use this module https://www.npmjs.com/package/uid, it generates variable length unique id
uid(10) => "hbswt489ts"
uid() => "rhvtfnt" Defaults to 7
Or you can have a look at this module https://www.npmjs.com/package/shortid
const shortid = require('shortid');
console.log(shortid.generate());
// PPBqWA9
Hope it works for you :)
var number = Math.floor(Math.random() * 9000000000) + 1000000000;
console.log(number);
This can be simplest way and reliable one.
For the length of 6, recursiveness doesn't matter a lot.
function random(len) {
let result = Math.floor(Math.random() * Math.pow(10, len));
return (result.toString().length < len) ? random(len) : result;
}
console.log(random(6));
In case you also want the first digit to be able to be 0 this is my solution:
const getRange = (size, start = 0) => Array(size).fill(0).map((_, i) => i + start);
const getRandomDigit = () => Math.floor(Math.random() * 10);
const generateVerificationCode = () => getRange(6).map(getRandomDigit).join('');
console.log(generateVerificationCode())
generate a random number that must have a fixed length of exactly 6 digits:
("000000"+Math.floor((Math.random()*1000000)+1)).slice(-6)
Generate a random number that will be 6 digits:
console.log(Math.floor(Math.random() * 900000));
Result = 500229
Generate a random number that will be 4 digits:
console.log(Math.floor(Math.random() * 9000));
Result = 8751
This code provides nearly full randomness:
function generator() {
const ran = () => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0].sort((x, z) => {
ren = Math.random();
if (ren == 0.5) return 0;
return ren > 0.5 ? 1 : -1
})
return Array(6).fill(null).map(x => ran()[(Math.random() * 9).toFixed()]).join('')
}
console.log(generator())
This code provides complete randomness:
function generator() {
const ran1 = () => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0].sort((x, z) => {
ren = Math.random();
if (ren == 0.5) return 0;
return ren > 0.5 ? 1 : -1
})
const ran2 = () => ran1().sort((x, z) => {
ren = Math.random();
if (ren == 0.5) return 0;
return ren > 0.5 ? 1 : -1
})
return Array(6).fill(null).map(x => ran2()[(Math.random() * 9).toFixed()]).join('')
}
console.log(generator())
parseInt(Math.random().toString().slice(2,Math.min(length+2, 18)), 10); // 18 -> due to max digits in Math.random
Update:
This method has few flaws:
- Sometimes the number of digits might be lesser if its left padded with zeroes.