简体   繁体   中英

Generate string of random number that includes some number and exclude some number

I have a problem about generating string in javascript. I have an array of number that string should contains atleast 1, and 1 number(up to 7 digits) that must not contains in a string. String length must be 7.

var incNumber = ["15","51","14","41","55","39","23"];
var exclude = ... //input from user

I tried to generate it by random select from array, random position and random other number around selected number. Then check if string contains excluded number, generate it again.

//random select number
var getRandom = incNumber[Math.floor(Math.random() * incNumber.length)];

//random position of number 
var position = Math.floor(Math.random() * 6);

//length of other string after selected number
var afterlen = 7 - (position+2);

//genNum(...) is my function that use to generate string of number in specific length.
var nstr = genNum(position)+getRandom+genNum(afterlen);

while (nstr.includes(exclude)) {
nstr = genNum(position)+getRandom+genNum(afterlen);
}

but doing this take too long time or sometimes freeze my browser. How should I fix it.?

edited: It's my homework about phonenumber. final string should be like "37915002"

Edited my code again

Does that now match your needs? It has got pretty messy and I'm not sure if it's correct (I'm never sure.. xD) hope you can get some inspiration though.

 // Variables var initialList = ["100", "5", "19", "88", "10", "90"]; var excludeList = ["9", "10"]; var resultLength = 7; var finalString = ""; // Create a third final array that is filtered var finalList = initialList.filter(element => { let shouldBeIncluded = true; excludeList.forEach(excluder => { Array.from(excluder).forEach(excluderFragment => { if (element.includes(excluderFragment)) shouldBeIncluded = false; }); }); if (shouldBeIncluded) return true; }); // Check if all were excluded if (finalList.length == 0) { // Do error handling here } else { // Create the list for (let i = 0; i < resultLength; i++) { finalString += finalList[Math.floor(Math.random() * finalList.length)]; } // Shorten the list because multiple digits values finalString = finalString.slice(0, 7); console.log(finalString); } 

You could start by filtering the unwanted number from the incNumber and doing everything the same way but on the new array

var incNumber = ["15","51","14","41","55","39","23"];
var exclude = "12";

var filteredNumbber =incNumber.filter(number=> number!==exclude);
var random = filteredNumbber[Math.floor(Math.random() * filteredNumbber.length)];

if we assume exclude is not a value but instead an array of values you would change the formula to

var incNumber = ["15","51","14","41","55","39","23"];
var exclude = ["15"];

var filteredNumbber =incNumber.filter(number=> !exclude.includes(number));
var random = filteredNumbber[Math.floor(Math.random() * filteredNumbber.length)];

as some people suggested random variable may end up as undefined if we exclude all the numbers inside of incNumber, if that was the case we should add an additional check in case that happens

if (random!==undefined) var nstr = genNum(position)+random+genNum(afterlen);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM