/    Sign up×
Community /Pin to ProfileBookmark

Unexpected behaviour in generating random number

I need to generate the phone number with starting digit 98.I use the below expression
98+”+Math.floor(Math.random()*100000000)

This generate 10 digit number .But sometimes it generate 9 digit .How to fix that problem

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorApr 08.2008 — I guees you should round to ceil and use 99999999 as limit. Try:
<i>
</i>Math.ceil(Math.random()*99999999)
Copy linkTweet thisAlerts:
@mrhooApr 08.2008 — It is possible to return a random number with a leading .0-

or even several zeroes before a digit.

It won't come up often, but once is probably too many.

To be absolutely sure your phone number will have 10 digits,

you better check it-


var s='98';

while(s.length!=10)s=98+''+Math.floor(Math.random()*1e8);
×

Success!

Help @d17may spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...