/    Sign up×
Community /Pin to ProfileBookmark

Create random number btw 1-50 with decimals?

How would I go about creating a random number between 1-50 that COULD include whole numbers, decimals to the tenth, hundredth and the thousandth place?

Ex) 2.145, 3, 5.8, 41.002, 10.12, 7, 18.023, 33.2

Also, if the only way to do it is to put trailing zeros to the thousandth place that’s fine as well

ex) 2.145, 3.00, 5.800, 41.002, 10.120, 7.00, 18.023, 33.200

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@cootheadMay 24.2011 — Hi there chunter,

and a warm welcome to these forums. ?

does this help...
[color=navy]
<script type="text/javascript">

var num=(Math.random()*49+1).toFixed(3);

alert(num);

</script>
[/color]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@chunterauthorMay 24.2011 — thank you very much.

Question though, it very rarely ever seems to hit a number with just 2 decimal places (ex 2.310) and almost never seems to hit a number with just 1 decimal place (ex 5.100) and I never can get it to hit a whole number.

You have given me an idea. Do you think it might be better/easier to create 4 random numbers?

a whole number, a number with 1 decimal, a number with 2 decimals and a number with 3 decimals and then just randomly select one of them?

thanks again.
Copy linkTweet thisAlerts:
@cootheadMay 24.2011 — Hi there chunter,

how often would you expect to see a whole number?

What do you want to achieve with 5000 random numbers?

[i]coothead[/i]
Copy linkTweet thisAlerts:
@mrhooMay 24.2011 — [CODE]function randomPrecision(lo, hi, prec){
prec= Math.floor(Math.random()*(prec+1));
return Number((lo+ Math.random()*(hi-lo+1)).toFixed(prec));
}[/CODE]


//test

var A= [], i= 100;

while(i--){

A.push(randomPrecision(1, 50, 3));

}

alert(A.join(', '))
Copy linkTweet thisAlerts:
@chunterauthorMay 24.2011 — Hi there chunter,

how often would you expect to see a whole number?

What do you want to achieve with 5000 random numbers?

[i]coothead[/i][/QUOTE]


whoa....you just opened my eyes. You're right. In essence, I am generating 50,000 numbers (I think? I'm still a little confused). The smallest being 1 and the largest being 50. But I also want all the decimals in between.

So, really the smallest would be 1.000 which would be like 1000?

And the largest being 50.000, which would be like 50000?

So, is that like the same as generating a random number between 1000 and 50000 and then dividing it by 1000?

I'm in the process of trying to build a worksheet generator. One that will create multiplication and division problems with decimals.

I love coding forums. You guys are so incredibly logical; you always force me to go back and look at the foundation of what it is I'm really asking for.
Copy linkTweet thisAlerts:
@JMRKERMay 24.2011 — I got sidetracked by lunch, but this is what I failed to post before going. :o
<i>
</i>&lt;!DOC HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Untitled &lt;/title&gt;
&lt;script type="text/javascript"&gt;
function RandDec() {
var randDec = ((1000+(Math.random() * 40000)) / 1000).toFixed(3);
alert(randDec);
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;button onclick="RandDec()"&gt;test&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@chunterauthorMay 24.2011 — I got sidetracked by lunch, but this is what I failed to post before going. :o
<i>
</i>&lt;!DOC HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Untitled &lt;/title&gt;
&lt;script type="text/javascript"&gt;
function RandDec() {
var randDec = ((1000+(Math.random() * 40000)) / 1000).toFixed(3);
alert(randDec);
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;button onclick="RandDec()"&gt;test&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;
[/QUOTE]


I think I realized that I want whole numbers to hit more often than what that would provide. So I guess I could just randomize the decimal place as well.

var num = Math.floor(Math.random()*(4));

var randDec = ((1000+(Math.random() *
50000)) / 1000).toFixed(num);

??
Copy linkTweet thisAlerts:
@JMRKERMay 24.2011 — I'm not sure of the purpose of this function, but perhaps you could approach the problem this way ...

Say you wanted integer random numbers 1-50 for 25% of the time

but you wanted decimal random numbers 1-50 for 75% of the time.

Create one array with 25 random integer numbers and add to that array 75 decimal random numbers. Then choose a random number 0-100 to pick from the integer and decimal combinations. Wa-La! You should be able to weight your integer-decimal picks!

Let us know if you need a script to accomplish that task, but it should be easy enough depending upon your requirements. ?

Something like this...
<i>
</i>&lt;!DOC HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Untitled &lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=1156489#post1156489

function RandDec() {
return ((1000+(Math.random() * 40000)) / 1000).toFixed(3);
}
function RandInt() {
return (Math.floor(Math.random() * 49))+1;
}
function RandDisplay() {
var ridArray = [];
for (var i=0; i&lt;25; i++) { ridArray.push(RandInt()); }
for (var i=0; i&lt;75; i++) { ridArray.push(RandDec()); }
var p = Math.floor(Math.random()*100);
// alert(ridArray.join(','));
// alert(ridArray[p]+'nn'+p);
return ridArray[p];
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;button onclick="alert(RandDisplay())"&gt;test&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@chunterauthorMay 25.2011 — Cross posted ...

see: http://www.codingforums.com/showthread.php?p=1093784#post1093784[/QUOTE]


each forum solved the problem differently. I hoped I didn't offend anyone by posting on both sites.
Copy linkTweet thisAlerts:
@chunterauthorMay 25.2011 — I don't understand something....I tried modifying the code, but something is not right.

This code doesn't work right:
<i>
</i>var x = 0;
while (x&lt;100){
var prec = Math.floor(Math.random()*(4));
var number = (1 + Math.random()*(50)).toFixed(prec);
document.write(prec+", "+number+"&lt;br&gt;");
x++;
}

It will result in some integers with decimals that only have zeros.

ex) 14.0, 5.0, 8.0


Your original code works perfect:

function randomPrecision(lo, hi, prec){
prec= Math.floor(Math.random()*(prec+1));
return Number((lo+ Math.random()*(hi-lo+1)).toFixed(prec));
}

var x=0;
while(x&lt;100){
document.write(randomPrecision(1, 50, 3)+"&lt;br&gt;");
x++;
}


It does not result in any integers with only zero in the decimal.

In other words, its smart enough to know that 14.0 should really just be 14, and 8.0 should just be 8.

I'm going to use it, but I really would like to know why this difference? The code is almost exactly the same. I'm missing something here.
×

Success!

Help @chunter 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.20,
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,
)...