-
Generate a random number, to a certain decimal point
Howdy is there anyway to generate a random number (within a range), to a certain decimal point and then display it in a text box?
I know how to do a full number, but am struggling to find out how you get random numbers with decimals.
Thanks in advance.
-
hi Pancreasboy
Code:
var num
newNum=random(100)
num=Math.random()*newNum
trace (num)
yesterday there was some debate about using random(number) as it is an older function that may not exist one day. But it works in this case....does that help?
-
Banned
Hi,
Here is a pretty useful thread regarding random usage with Flash...
http://board.flashkit.com/board/showthread.php?t=690615
-
edit: oops, didnt notice "decimal"
Last edited by moagrius; 06-30-2006 at 05:22 AM.
-
ok so heres the code im using atm
function shuffle(){
return Math.floor(Math.random() * 2);
}
var allNumbers=[];
for(var i=10;i<=20;i++){
allNumbers.push(i);
}
allNumbers.sort(shuffle);
results=[];
for(var r=0;r<1;r++){
results.push(allNumbers[r]);
}
trace(results);
display1.text=results[0];
so instead of the *2 should i have return Math.floor(Math.random() * 100)/100;
sorry im more of a designer rather than a programmer
-
here's a couple ways - the first is the commonly accepted approach (with some minor speed tweaks), but you'll find that it'll fail in flash with large numbers. the second's functionality is entirely within string manipulation show should always return an appropriate productcode: Number.prototype.toFixed=function(a){
var b=Math.pow;
return (this|0)+"."+(b(10,a)+(0.5+(this-(this|0))*b(10,a))+"").substr(1,a);
}
Number.prototype.toFixed=function(a){
var b=(""+this).split(".");
b[1]=[0.5+(b[1].substr(0,a+1)*0.1)|0]+"";
while(b[1].length<a)b[1]=b[1]+0;
return b.join(".");};
var A=123.23498620398;
var B=0.236;
var C=0.233;
var D=100;
trace(A.toFixed(5));
trace(B.toFixed(2));
trace(C.toFixed(2));
trace(D.toFixed(18));
-
God
the easyest way is just to go
_root.ranDecimal = (random(1000000))/1000
that should find a random number between 0 and 100 and it will be to the tenthousandths place (4 numbers past the decimal)
-
All 1s and 0s
Hi,
Here's a nice method that doesn't require string manipulation. The part you're interested in is how to get the required decimal places. The answer is simple! Mutliply the random number by 10^d (where d is the required number of decimal places), take the floor, and then divide by 10^d. For exmaple, let's say our number is:
354.5679483722;
and we want to 5 decimal places. Mutliply by 10^5 (100000):
34556794.83722;
take the floor:
34556794;
and then divide by 10^5:
345.56794;
Here's the function:
code:
function rnd(a:Number, b:Number, d:Number):Number {
//Get a random number;
var r:Number = Math.random();
//Put the number in range;
r = r*(b-a) + a;
//Round to the required number of decimal places;
r = Math.floor(r*Math.pow(10,d))/Math.pow(10,d);
//Return r;
return r;
}
The syntax is rnd(lowerLimit:Number, upperLimit:Number, decimalPlaces:Number). Let's say you wanted a number between 120 and 270 at 6 decimal places:
var myRandom:Number = rnd(120, 270, 6);
Hope this helps.
NB: The method I use here means that the entire number is restricted to 15 significant figures. If you want more, let me know. There'll be a work around!
"If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton
-
those dont pad.
you need a string, not a number, to get back 103.20 (e.g., $103.20 versus $103.2)
-
... and string manipulation is faster than 3 lookups to the Math class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|