Generate 4-digit aléatory numbers [0000-9999]

Asked

Viewed 1,464 times

-2

Follow the example below of what is happening, I declared 1198805- and the rest is generated. But with this defect below.

1198805522
11988052636
11988054970
1198805929
11988054269
11988054150
1198805671
1198805848
11988055359

Follow the Code in Javascript used :

var maximo = 8999;
var resultados = 40;
var stop;
var contador = 0;
	//var txt = document.getElementById("txtsenha").value;
    var txt = "11" + "" + "9" + "9204" + "-";
   // document.getElementById(el).innerHTML += txt + x + "<br>";
	document.getElementById('contadorrr').innerHTML = '<font color=#FFF size="6"> <b>-</b>Loanding<b>-</b> </font><font size="29">[</font><font size="59" color=#1E90FF> ' + contador + '</font><font size="29">]</font><font size="29" color=#1E90FF>' ;

var i, arr = [];
for (i = 0; i < maximo; i++) {
    arr[i] = i + 1;
}

var p, n, tmp;
for (p = arr.length; p;) {
    n = Math.random() * p-- | 0;
    tmp = arr[n];
    arr[n] = arr[p];
    arr[p] = tmp;
	}

for (var i = 0; i < resultados; i++) {
 document.getElementById('out').innerHTML += txt + arr[i]+ '<br> ';
}
			</head>
<body style="background-image:url(https://i.ytimg.com/vi/gJ16uN47EAk/maxresdefault.jpg);">
<hr>
        <div class="configdiv" align="center"  style="color: #F8F8FF;">
<p><font size="15">-</font> <font size="7" color="#B5B5B5">SPACE GHOST V1.0</font> <font size="15">-</font></p> 

                <img align="center" src="http://img15.deviantart.net/49f9/i/2015/167/5/2/space_ghost_logo_by_topher147-d8x52wm.png" width="300" height="250" >
				
				
<div id="contadorrr"><font size="20" color="#FFD700"><strong></strong></font></div>
<br><br><HR>


<hr>

<font size="20" color="#20B2AA">[</font> <font size="20" color="#D3D3D3"><strong> NUMEROS </strong></font> <font size="20" color="#20B2AA">]</font>
<div id="out" align="center"  style="color: #00BFFF;"></div>
<hr></hr>
<div id="interval" align="center" style="color: #00FF00;"></div>

The 4 digits drawn, total 5040 different combinations without repetition However in my code it generates broken lines of 10, sometimes of 12 digits, and this interferes in the total of REAL results (11 digits)

Does anyone know how to limit the code to generate only 11 digits ?

Thank you.

  • As already explained in the deleted version of the same question, the essential thing is this: Fill left zeros in Javascript

  • And the shuffling without repeating part is here: Random numbers without repetition

  • Thanks @Bacco was sorry for the other question, I had not understood what you went through , thanks msm

  • The important thing is that it solved. Over time you will get the feel of the site and enjoying better. It is that almost every time we have something ready on the site, we already point, both to advance the solution when possible, as to help other colleagues with the same problem (and when there is indication of duplicate, it is mere organization, to concentrate all the answers in one place, it is not punishment and not sign of anything necessarily wrong with your question). Whenever you get in doubt or find something strange, just leave a comment (not in the body of the question) that there are plenty of people wanting to help.

  • And as soon as you have a few more dots, you will be able to join the network chat, which is a quick way to ask questions about how to improve or find a post about a particular subject, or any doubt or idea that doesn’t fit well in the question-answer format.

  • Thanks for the attention, I’ve done my part and released the chat, pity that not so many people interested in javascript , but it will be much more useful to use the chat really ,to reading a booklet I found on google, but there is something that only someone explaining even to learn, if you are from the area of boars , will see me mt through here kkk

  • In business hours brings more people together. Chat is good for more interactive things. Objective questions are good to ask right here, so it helps more people, and it gets organized. If you have interest and time, we have [help] that has some cool tips, and has the site meta, which is "the site on the site". In the goal we do not discuss programming itself, but discuss the standards of the site, what is accepted or not, and help shape the community. It is a good source of reading on the behaviors and methodologies used in the main site.

Show 2 more comments

1 answer

4


function pad(str, length) {
  const resto = length - String(str).length;
  return '0'.repeat(resto > 0 ? resto : '0') + str;
}

var resultados =5040;
var maximo = 9999;
var arr = [];
while(arr.length < resultados){
    var random = Math.floor(Math.random() * maximo)
    random = pad(random, 4);
    if(arr.indexOf(random) > -1) continue;
    arr[arr.length] = random;
    console.log("11" + "" + "9" + "8805" + random);
}

  • Dude but so it won’t duplicate the results? taking into account that 4 digits is total of 5,040 combinations(counting with ZERO), my code already avoids duplicate for me to get the total result , however it breaks the digits from 4 to 3 or from 4 to 5 (as a count and it continues counting....) , I want to avoid this and determines TOTAL result of 11 digits and 4 digits generated and without duplicates . what if I put zero in front of generated ? or use something similar like length where [0000-9999] and not [0-9999]

  • +1, solved the pad and the range after the last Edit. is the suggestion of who will use in practice to exchange the raffle algorithm only, but this is an implementation detail.

  • Bacchus I edited the answer according to his guidelines. Actually Fisher-Yates is much more performative (up to 10x and 8x in the case of 5040 numbers) which would result in 3 milliseconds of difference. OBS: I made these observations according to this analysis of Fisher-Yates x Rute force: http://www.freefour.com/analysis-of-a-brute-force-shuffle/

  • was much faster even , great tip

Browser other questions tagged

You are not signed in. Login or sign up in order to post.