Random No. Generator (SHOULD BE 4 DIGIT) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Random No. Generator (SHOULD BE 4 DIGIT)

Any help would be much appreciated 😄 no.s like 0003 , 0020 , 0400 , 5000 r okay but not 0000

9th May 2017, 12:14 PM
_Retr0/-
_Retr0/- - avatar
8 Answers
+ 8
This isn't 4-digit, nor is it in JS, but might help you get an idea of achieving your aims. https://code.sololearn.com/cuumeZ0dI9iJ/?ref=app
9th May 2017, 12:23 PM
Hatsy Rei
Hatsy Rei - avatar
+ 7
I have been trying but I the problem is making it 4 digit
9th May 2017, 12:14 PM
_Retr0/-
_Retr0/- - avatar
+ 7
num = '""+(1+Math.floor(9999*Math.random())); while (num.length<4) num = "0"+num; alert(num); [edit] :D replaced "print" by "alert"... too many Python at same time as JS ^^
9th May 2017, 12:38 PM
visph
visph - avatar
+ 6
Your solution require little adaptation: only the most left digit cannot be 0 ;)
9th May 2017, 12:50 PM
visph
visph - avatar
+ 6
Yes :) ... but mine solution is more efficient, as it require only one call to random() for generating the number, rather four in yours ;P
9th May 2017, 1:01 PM
visph
visph - avatar
+ 6
Thx for all the solution guys I will try them
9th May 2017, 2:03 PM
_Retr0/-
_Retr0/- - avatar
+ 5
Yes, but you need this only for the most left digit, not the others ^^
9th May 2017, 12:56 PM
visph
visph - avatar
+ 4
Well you can tweak this when displaying the number.You could for instance check the length of the generated number. If it is a two digits number, you add two zeros before it. If it is a one digit, you add three zeros before... etc. that's how I personally see it.
9th May 2017, 12:19 PM
CHMD
CHMD - avatar