so I know this questing has been asked, but all the answers that were given are already known to me. I don't want to make a variable of all the posible numbers (that was always the answer). So to go to the question, I want to make a random number generator, that will generate me 7 numbers, that must not be the same. For example, I get random numbers:
"5,16,12,5,21,37,2" ... But I don't want the number 5 to be used again, so I want different numbers. I made a code for the generation, but I can not think of any good method/way to do this. I was thinking that maybe check if the number is already in array and if it is, then generate another number, but as I'm amateur in JavaScript, I don't know how to do this. So here is my JavaScript code:
// JavaScript Document
function TableOn()
{
document.write("<table border='1'>");
}
function TableOff()
{
document.write("</table>");
}
function RandNum()
{
var n = new Array();
for(var i=0;i<7;i++)
{
n[i] = Math.round((1+(Math.random()*40)));
}
TableOn();
for(var c=0;c<7;c=c+1)
{
document.write("<tr><td>"+n[c]+"</td></tr>");
}
TableOff();
}
In HTML I just have a button, that is onclick="RandNum()" ... Pardon for my English.
I would do it like this:
var nums = [], numsLen = 5, maxNum = 100, num;
while (nums.length < numsLen) {
num = Math.round(Math.random() * maxNum);
if (nums.indexOf(num) === -1) {
nums.push(num);
}
}
This generates an array with 5 random numbers in the range 0..100.
(numsLen cannot be greater than maxNum.)
These commands can be used to check if a value is/is not in your array:
if ( !!~n.indexOf(someVal) ) {
// someVal is in array "n"
}
if ( !~n.indexOf(someVal) ) {
// someVal is not in array "n"
}
I'd use a string, storing the generated random numbers with a divider. Then check if the newly generated number is in that string.
Something like this
generated = "";
for(var i=0;i<7;i++)
{
generate = Math.round((1+(Math.random()*40))); //generate = 5
while (generated.indexOf("[" + generate + "]") != -1) { //checking if the [5] is already in the generated string, and loop until it's a different number
generate = Math.round((1+(Math.random()*40))); //get a new random number
}
generated += "[" + generate + "]";
n[i] = generate;
}
or you can take another longer approach
for(var i=0;i<7;i++)
{
repeated = true;
while (repeated) {
repeated = false;
generate = Math.round((1+(Math.random()*40)));
for (var a=0; a < i, a++) {
if (generate == n[a]) { repeated = true; }
}
}
n[i] = generate;
}
Here's a function to generate an array of n unrepeated random numbers in [min, max):
function rands(n, min, max) {
var range = max - min;
if (range < n)
throw new RangeError("Specified number range smaller than count requested");
function shuffle() {
var deck = [], p, t;
for (var i = 0; i < range; ++i)
deck[i] = i + min;
for (i = range - 1; i > 0; --i) {
p = Math.floor(Math.random() * i);
t = deck[i];
deck[i] = deck[p];
deck[p] = t;
}
return deck.slice(0, n);
}
function find() {
var used = {}, rv = [], r;
while (rv.length < n) {
r = Math.floor(Math.random() * range + min);
if (!used[r]) {
used[r] = true;
rv.push(r);
}
}
return rv;
}
return range < 3 * n ? shuffle() : find();
}
This code checks the range of possible values and compares it to the number of random values requested. If the range is less than three times the number of values requested, the code uses the shuffle to avoid the terrible performance of the lookup approach. If the range is large, however, the lookup approach is used instead.
Not sure if OP's requirement of non-repeating is really needed, but here's a fiddle of something that could work if your number range isn't too big:
http://jsfiddle.net/6rEDV/1/
function range(start, end, step) {
if (typeof step === 'undefined') {
step = 1;
}
if (typeof start === 'undefined' || typeof end === 'undefined') {
throw TypeError('Must have start and end');
}
var ret = [];
for (var i = start; i <= end; i += step) {
ret.push(i);
}
return ret;
};
// source: http://stackoverflow.com/a/6274381/520857
function shuffle(o) { //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
function getNonRepeatingRand(min, max, num) {
var arr = shuffle(range(min, max));
return arr.slice(0, num-1);
}
// Get 7 random numbers between and including 1 and 1000 that will *not* repeat
console.log(getNonRepeatingRand(1,1000,7));
A possibly slower, but less memory intensive method:
http://jsfiddle.net/Qnd8Q/
function rand(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getNonRepeatingRand(min, max, num) {
var ret = [];
for (var i = 0; i < num; i++) {
var n = rand(min, max);
if (ret.indexOf(n) == -1) {
ret.push(n);
} else {
i--;
}
}
return ret;
}
console.log(getNonRepeatingRand(1,5,5));
var n = new Array(),num;
function TableOn()
{
document.write("<table border='1'>");
}
function TableOff()
{
document.write("</table>");
}
function check_repition()
{
num=Math.round((1+(Math.random()*40)))
if(n.indexOf(num)==-1)
return true;
else
return false;
}
function RandNum()
{
for(var i=0;i<7;i++)
{
if(check_repition())
n[i] =num;
else // keep checking
{
check_repition()
n[i] =num;
}
}
TableOn();
for(var c=0;c<7;c=c+1)
{
document.write("<tr><td>"+n[c]+"</td></tr>");
}
TableOff();
}
RandNum()
Related
I am trying to choose random unique numbers everytime when I click button. For this my function is:
const chooseNumber = () => {
var r = Math.floor(Math.random() * 75) + 1;
console.log(r)
while(selectedNumbers.indexOf(r) === -1) {
selectedNumbers.push(r);
}
console.log(selectedNumbers);
};
But the problem is if the random number is already on my list, I need to click the button again to generate new number and it goes until it find the number which is not on the list. But I want to generate number which is not on the list directly so I dont need to click the button everytime. Thanks for you helps.
You are in a right track, except the while loop should be for random number generator, not pushing number into an array:
const selectedNumbers = [];
const chooseNumber = () => {
let r;
do
{
r = Math.floor(Math.random() * 75) + 1;
}
while(selectedNumbers.indexOf(r) > -1)
selectedNumbers.push(r);
console.log(r, "["+selectedNumbers+"]");
};
<button onclick="chooseNumber()">Generate</button>
Note, that this might eventually lead to a freeze, since there is no fail safe check if array is full, so to battle that we should also check length of the array:
const selectedNumbers = [];
const maxNumber = 75;
const chooseNumber = () => {
let r;
do
{
r = ~~(Math.random() * maxNumber) + 1;
}
while(selectedNumbers.indexOf(r) > -1 && selectedNumbers.length < maxNumber)
if (selectedNumbers.length < maxNumber)
selectedNumbers.push(r);
else
console.log("array is full");
console.log(r, "["+selectedNumbers+"]");
};
for(let i = 0; i < 76; i++)
{
chooseNumber();
}
<button onclick="chooseNumber()">Generate</button>
Don't rely on a loop to generate a unique (unseen) integer in a limited range.
First, once all of the values in the range have been exhausted there will be no possibilities left, so you'll be left in an endless loop on the next invocation.
Second, it's wasteful of the processor because you are generating useless values on each invocation.
Instead, generate all of the values in range in advance (once), then shuffle them and get the last one from the array on each invocation (and throw an error when none remain):
/**
* Durstenfeld shuffle
*
* - https://stackoverflow.com/a/12646864/438273
* - https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
function shuffleArray (array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
/** Get shuffled array of all integers in range */
function generateShuffledIntegerArray (min, max) {
const arr = [];
for (let i = min; i <= max; i += 1) arr.push(i);
shuffleArray(arr);
return arr;
}
const getUniqueInt = (() => {
const numbers = generateShuffledIntegerArray(1, 75);
return () => {
const n = numbers.pop();
if (typeof n === 'number') return n;
throw new Error('No unique numbers remaining');
};
})();
// Will log 75 integers, then throw on the 76th invocation:
for (let i = 1; i <= 76; i += 1) {
const n = getUniqueInt();
console.log(`${i}:`, n);
}
Code in TypeScript Playground
Your while loop is unnecessary. You could use an "if" statement instead.
To avoid clicking again on your button, you can do a recursive function like:
const chooseNumber = () => {
var r = Math.floor(Math.random() * 75) + 1;
console.log(r)
if(selectedNumbers.indexOf(r) === -1) {
selectedNumbers.push(r);
console.log(selectedNumbers);
} else {
chooseNumber();
}
};
This question already has answers here:
Generate unique random numbers between 1 and 100
(32 answers)
Closed 4 years ago.
This has been asked dozens of times, but somehow, after reading many answers, I'm not convinced. I'm not cleared about the best way to do it, performance and code simplicity.
Should I set the list [1.. 100] and keep picking random (it will run 10 times) from there to another array, avoiding searching for it every new random?
Should I develop and run 10 times (at least) a random function to return a 1.. 100, checking if it is not a dupe and put it into an array?
Some Javascript function that I'm missing?
Thanks
You can use a while loop to generate random numbers with Math.random() and add the numbers to a Set which contains only unique values.
var randoms = new Set();
while(randoms.size<10){
randoms.add(1 + Math.floor(Math.random() * 100));
}
console.log([...randoms.values()]);
You can also just use an Array and check if the generated random number already exists in it before pushing it to the Array.
var randoms = [];
while(randoms.length<10){
var random = Math.ceil(1 + Math.floor(Math.random() * 100));
if(randoms.indexOf(random)==-1){
randoms.push(random);
}
}
console.log(randoms);
For a more generic function, you can use this:
function generateRandoms(min, max, numOfRandoms, unique){
/*min is the smallest possible generated number*/
/*max is the largest possible generated number*/
/*numOfRandoms is the number of random numbers to generate*/
/*unique is a boolean specifying whether the generated random numbers need to be unique*/
var getRandom = function(x, y){
return Math.floor(Math.random() * (x - y + 1) + y);
}
var randoms = [];
while(randoms.length<numOfRandoms){
var random = getRandom(min, max);
if(randoms.indexOf(random)==-1||!unique){
randoms.push(random);
}
}
return randoms;
}
function generateRandoms(min, max, numOfRandoms, unique){
var getRandom = function(x, y){
return Math.floor(Math.random() * (x - y + 1) + y);
}
var randoms = [];
while(randoms.length<numOfRandoms){
var random = getRandom(min, max);
if(randoms.indexOf(random)==-1||!unique){
randoms.push(random);
}
}
return randoms;
}
console.log(generateRandoms(1, 100, 10, true));
This technique creates N1 numbers (the total range) and shuffles them, then picks the top N2 number (how many we actually want), we'll use Fisher-Yates shuffle.
const n1 = 100;
const n2 = 10;
let pool = [...Array(n1).keys()];
var result = [];
while (result.length < n2) {
let index = Math.floor(Math.random() * pool.length);
result = result.concat(pool.splice(index, 1));
}
console.log(result);
var randomArray = [];
while(randomArray.length < 10) {
var random = Math.round(Math.random() * 100);
if(randomArray.indexOf(random) === -1) {
randomArray.push(random);
}
}
console.log(randomArray);
#2 would be the most efficient.
var nums = []
while(nums.length < 10) {
var n = Math.round(Math.random()*100);
if (!nums.includes(n)) nums.push(n);
}
console.log(nums);
You could also use Set in a newer browser, which would be a little faster than manually checking for existence:
var nums = new Set();
while(nums.size < 10) {
var n = Math.round(Math.random()*100);
nums.add(n);
}
console.log([...nums.values()]);
This function adds all numbers from betweenStart to betweenEnd, randomizes them over randomRuns loops and returns a list with amount entries:
function randomNumbersBetweenXAndY(betweenStart, betweenEnd, amount, randomRuns) {
if (betweenStart === void 0) { betweenStart = 0; }
if (betweenEnd === void 0) { betweenEnd = 100; }
if (amount === void 0) { amount = 10; }
if (randomRuns === void 0) { randomRuns = 1; }
//Verify parameters
var maxPossibleCandidates = Math.abs(betweenStart - betweenEnd) + 1;
if (amount > maxPossibleCandidates) {
console.warn("You cannot get more unique numbers between " + betweenStart + " and " + betweenStart + " than " + maxPossibleCandidates + ". " + amount + " is too many!");
amount = maxPossibleCandidates;
}
//array to return
var list = [];
//fill array
for (var index = betweenStart; index <= betweenEnd; index++) {
list.push(index);
}
//Randomize
while (randomRuns--) {
for (var index = 0; index < list.length; index++) {
var randomIndex = Math.floor(Math.random() * list.length);
var tmp = list[index];
list[index] = list[randomIndex];
list[randomIndex] = tmp;
}
}
//Return data
return list.slice(0, amount);
}
//TEST
console.log(randomNumbersBetweenXAndY(1, 100, 10));
I am having a little issue writing a function that factorizes numbers. The hard part is done. However I cannot seem to tell the function to return 1 when num is 0.
PS: which other ways would you write the same function in JavaScript?
var arrOfNum = [];
function factorialize(num) {
for(i = 1; i <= num; i++){
// push all numbers to array
arrOfNum.push(i);
}
// multiply each element of array
var result = arrOfNum.reduce(function(a,b){
return a * b;
});
console.log(result);
}
You already have a for loop, in which you can calculate the factorial at once, without array and reduce.
function factorial(num) {
var result = 1;
for(i = 2; i <= num; i++) {
result *= i;
}
return result;
}
You can use the following method that uses the recursion:
function factorize(num){
if(num === 0){
return 1 ;
}
else {
return num = num * factorize(num-1);
}
}
Roundup:
Declaration of local variable i is missing
var i;
Declaration of other used variables are over the function distributed. A better way is to declare the variables at top of the function.
Array#reduce needs for this task an initialValue as the second parameter.
The first time the callback is called, previousValue and currentValue can be one of two values. If initialValue is provided in the call to reduce, then previousValue will be equal to initialValue and currentValue will be equal to the first value in the array. If no initialValue was provided, then previousValue will be equal to the first value in the array and currentValue will be equal to the second.
function factorial(num) {
var i,
arrOfNum = [],
result;
for (i = 1; i <= num; i++) {
// push all numbers to array
arrOfNum.push(i);
}
// multiply each element of array
result = arrOfNum.reduce(function (a, b) {
return a * b;
}, 1);
document.write(num+'! = '+result + '<br>');
}
factorial(0);
factorial(1);
factorial(2);
factorial(5);
factorial(8);
Simply return the value 1
function factorialize(num) {
if (num < 1) return 1; //this line is added
for(i = 1; i <= num; i++){
arrOfNum.push(i);
}
var result = arrOfNum.reduce(function(a,b){
return a * b;
});
console.log(result);
}
If you give reduce an initial value of 1, everything will work fine even without an explicit check:
var result = arrOfNum.reduce(function(a,b){
return a * b;
}, 1);
^^^ // PROVIDE EXPLICIT INITIAL VALUE TO REDUCE
function factorial(n) {
return Array.apply(0, Array(n)).reduce(function(x, y, z) {
return x + x * z; //1+(1*0), 1+(1*1),2+(2*2), 6+(6*3), 24+(24*4), ...
}, 1);
}
DEMO
Here's a fairly streamlined function that returns an array of all factors of 'n'
You only need to look at candidates < sqrt(n)
For those of you who don't know the | 0; bit when getting sqrt(n) is a faster equivalent of Math.floor()
As factn is defined after some sanity checking the function will either return undefined or an array which is easy to check with something like if(factors = factorize(n) { success code } sorta structure
There are improvements that can be made to this but they're complex and were beyond the requirements when I wrote it - specifically I used this to calculate CSS sprite sizes from a large image by using factorize on the x + y dimensions of an image then creating a third array of shared factors (which gives you a list of all the possible square sprite sizes).
function factorize(n) {
n = Number(n);
if(n) {
if(n > 1) {
var sqrtn = Math.sqrt(n) | 0;
var factn = [1, n];
var ipos = 0;
for(i = 2; i <= sqrtn; i++) {
if((n % i) == 0) {
ipos++;
if((n / i) !== i) {
factn.splice(ipos, 0, i, n / i);
} else {
factn.splice(ipos, 0, i);
}
}
}
}
}
return factn;
}
Don't know why there are complicated answers. A very simple answer is:
var i;
function factorialOf(num) {
//Initially set factorial as number
var factorial = num;
//A for loop starting with 1 and running the times equal to num
for (i = 1; i < num; i++) {
//Set factorial to the number itself * i
factorial = factorial * i;
}
//Return factorial
return factorial;
}
console.log(factorialOf(5));
I have an annoying script I can't complete.
I need 32 non-repeating numbers out of a possible 0-64 number set. Every time I try to create a loop to check the new random number against all the numbers in the array I end up with nothing, or an infinite loop.
I'm stumped :(
Try this:
var keys = [], numbers = [], x, total = 0;
while(total < 32) {
x = Math.floor(Math.random() * 64);
// way faster that looping through the array to check if it exists
if(keys[x] == undefined) {
keys[x] = 1;
numbers.push(x);
total++;
}
}
console.log(numbers);
Working Demo
This code will generate a non repeating random number between 0 and whatever number you give it and will start over when it has been called the amount of the number you give it.
Give it a try:
let randomNumber = function (max) {
let min = 0, prevIndexes = [];
function exec(max2) {
max = max || max2;
let result = Math.floor(Math.random() * (max - min + 1) + min);
if (prevIndexes) {
if (prevIndexes.length - 1 === max) {
clear();
}
let foundDouble, eqPrevInn = true;
while (eqPrevInn) {
foundDouble = false;
result = Math.floor(Math.random() * (max - min + 1) + min);
for (let i = 0, l = prevIndexes.length; i < l; i++) {
if (result === prevIndexes[i]) {
foundDouble = true;
break;
}
}
if (!foundDouble) {
eqPrevInn = false;
}
}
}
prevIndexes.push(result);
console.log(prevIndexes);
return result;
}
let clear = function () {
if (prevIndexes) {
prevIndexes = [];
// console.log('prevIndexes has been cleared');
}
else {
// console.log('already clear');
}
}
return {
exec: exec,
clear: clear
}
};
let random32 = randomNumber(32/*range*/).exec;
for (let i = 0; i <= 32; i++) {
random32();
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is JavaScript's Math broken?
The user enters values in the first two text boxes and, as they type, Javascript (sorry, no jQuery, I'm not up to it yet) is used to calculate the precise sum and the sum rounded to 2 digits.
Why am I getting rounding error and what can I do to correct it?
Many thanks.
Hmmm....ParseFloat? Wrong data type?
What I would like to see if the precise answer as if it were added on a calculator. Is there a parseDecimal or other data type that I can use?
![enter image description here][1]
function SumValues() {
//debugger;
var txtSubsContrRbtAmt = document.getElementById("<%=txtSubsContrRbtAmt.ClientID%>");
var txtDeMinAmt = document.getElementById("<%=txtDeMinAmt.ClientID%>");
var txtTotRbtAmt = document.getElementById("<%=txtTotRbtAmt.ClientID%>");
var txtRndRbtAmt = document.getElementById("<%=txtRndRbtAmt.ClientID%>");
var total = Add(txtSubsContrRbtAmt.value, txtDeMinAmt.value);
txtTotRbtAmt.value = total;
txtRndRbtAmt.value = RoundToTwoDecimalPlaces(total);
}
function Add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
var currentValue;
if (isNumber(arguments[i])) {
currentValue = parseFloat(arguments[i]);
}
else {
currentValue = 0;
}
sum += currentValue;
}
return sum;
}
function RoundToTwoDecimalPlaces(input) {
return Math.round(input * 100) / 100
}
function IsNumeric(input) {
return (input - 0) == input && input.length > 0;
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
[1]: http://i.stack.imgur.com/5Otrm.png
Update. I am evaluating something like this:
function AddWithPrecision(a, b, precision) {
var x = Math.pow(10, precision || 2);
return (Math.round(a * x) + Math.round(b * x)) / x;
}
There is a golden rule for anyone writing software in the financial sector (or any software dealing with money): never use floats. Therefore most software dealing with money use only integers and represent decimal numbers as a data structure.
Here's one way of doing it:
(Note: this function adds two strings that looks like numbers)
(Additional note: No error checking is done to aid clarity. Also does not handle negative numbers)
function addNumberStrings (a,b) {
a = a.split('.');
b = b.split('.');
var a_decimal = a[1] || '0';
var b_decimal = b[1] || '0';
diff = a_decimal.length - b_decimal.length;
while (diff > 0) {
b_decimal += '0';
diff --;
}
while (diff < 0) {
a_decimal += '0';
diff ++;
}
var decimal_position = a_decimal.length;
a = a[0] + a_decimal;
b = b[0] + b_decimal;
var result = (parseInt(a,10)+parseInt(b,10)) + '';
if (result.length < decimal_position) {
for (var x=result.length;x<decimal_position;x++) {
result = '0'+result;
}
result = '0.'+result
}
else {
p = result.length-decimal_position;
result = result.substring(0,p)+'.'+result.substring(p);
}
return result;
}
*note: code is simplified, additional features left out as homework.
To fix your addition the way you want, I'd suggest counting the decimal places in each number somehow This method, for instance Then passing the max value to toFixed, and trimming any leftover zeroes.
function AddTwo(n1, n2) {
var n3 = parseFloat(n1) + parseFloat(n2);
var count1 = Decimals(n1, '.');
var count2 = Decimals(n2, '.');
var decimals = Math.max(count1, count2);
var result = n3.toFixed(decimals)
var resultDecimals = Decimals(result, '.');
if (resultDecimals > 0) {
return result.replace(/\.?0*$/,'');
}
else {
return result;
}
}
// Included for reference - I didn't write this
function Decimals(x, dec_sep)
{
var tmp=new String();
tmp=x;
if (tmp.indexOf(dec_sep)>-1)
return tmp.length-tmp.indexOf(dec_sep)-1;
else
return 0;
}
Here's a JSFiddle of that