This content has been marked as final.
Show 12 replies

1. Re: How to make a random name appear
DazFaz Oct 12, 2006 3:35 AM (in response to anubia)var myNames:Array = new Array("bob","fred","Mr Smith","noddy","Charlton and the wheelies");
callMeaName = function(){
return myNames[random(myNames.length)];
}
trace(callMeaName()) 
2. Re: How to make a random name appear
Newsgroup_User Oct 12, 2006 8:21 AM (in response to anubia)First I was surprised it did work, then I thought of the old random
function.
the random function is deprecated. Math.random is the way to go now.
Using it would result in code below:
var myNames:Array = new Array("bob","fred","Mr Smith","noddy","Charlton
and the wheelies");
callMeaName = function(){
return myNames[Math.floor(Math.random()*myNames.length)];
}
trace(callMeaName())
Lengthier? Yes... (But it'll last longer :)
Too bad they don't give dates or version numbers when something will be
phased out instead of just deprecated.
Manno
DazFaz wrote:
> var myNames:Array = new Array("bob","fred","Mr Smith","noddy","Charlton and the wheelies");
> callMeaName = function(){
> return myNames[random(myNames.length)];
> }
> trace(callMeaName())


Manno Bult
manno@xs4all.nl 
3. Re: How to make a random name appear
DazFaz Oct 14, 2006 6:16 AM (in response to anubia)The problem with using Math.foor is that it will only round down to the nearest number and not give a true random number in all acounts:
Manno wrote:
>var myNames:Array = new Array("bob","fred","Mr Smith","noddy","Charlton
and the wheelies");
>callMeaName = function(){
>return myNames[Math.floor(Math.random()*myNames.length)];
}
>trace(callMeaName())
>Lengthier? Yes... (But it'll last longer :)
>Too bad they don't give dates or version numbers when something will be
>phased out instead of just deprecated.
To create a true random number you should use Math.round(). This will round up or down to the nearest integer:
var myNames:Array = new Array("bob", "fred", "Mr Smith", "noddy", "Charlton and the wheelies");
Array.prototype.randomThis = function() {
s = this[Math.round(Math.random()*(this.length1))]
return s
};
trace(myNames.randomThis());
Manno was nearly there and is right about using code that will not be outdated to soon.
DazFaz 
4. Re: How to make a random name appear
Newsgroup_User Oct 19, 2006 8:31 AM (in response to anubia)
Hi DazFaz,
was browsing and found your reply...
In what way do you mean: "will not give a true random number in all
acounts"?
Is that something internal to the rounding of numbers?
Although ofcourse not the same in result, both methods _seem_ pretty
random to me:
var ar:Array = new Array("random", "floor", "ceiling");
var rnd:Number = Math.random();
var rndV1 = Math.floor(rnd*ar.length);
var rndV2 = Math.round(rnd*(ar.length1));
trace(rnd + "\t" + rndV1 + "\t" + rndV2);
kind regards,
Manno
DazFaz wrote:
> The problem with using Math.foor is that it will only round down to the nearest
> number and not give a true random number in all acounts:
> >return myNames[Math.floor(Math.random()*myNames.length)];
> To create a true random number you should use Math.round(). This will round up
> or down to the nearest integer:
>
> Array.prototype.randomThis = function() {
> s = this[Math.round(Math.random()*(this.length1))]
> return s
> };


Manno Bult
manno@xs4all.nl 
5. Re: How to make a random name appear
DazFaz Oct 19, 2006 9:21 AM (in response to Newsgroup_User)Hi Manno Bult.
The problem with user Math.floor is that it only moves in one direction and that is down to an integer.
With Math.round ir rounds off the number to the nearest integer.
If you run the code I have modified from yours, you will see what I mean. You will notice that rndV1 always stays at 0 yet rndV2 will deviate from 0 to 1. Making it a more accurate way of finding a true random number. 
6. Re: How to make a random name appear
Newsgroup_User Oct 19, 2006 1:41 PM (in response to Newsgroup_User)Hi DazFaz,
true, but with an array with length of 1 rounding down prevents an index
out of range error.
In you original code you 'round down' by multiplying by the length of
the array 1 while I round down with Math.floor (and multiply by the
actual length of the array).
Is there a difference in result?
The code below can yield 1 while the array may just be 1 item long:
var arrayLength:Number = 1;
var rnd:Number = Math.random() * arrayLength;
var rndV1 = Math.floor(rnd);// Rounds ONLY down to the nearest integer
var rndV2 = Math.round(rnd);// rounds up OR down to the nearest integer
trace(rnd + "\t" + rndV1 + "\t" + rndV2);
so either decrement the length or floor the number. Same difference (No?)
I love puzzels :)
DazFaz wrote:
> Hi Manno Bult.
>
> The problem with user Math.floor is that it only moves in one direction and
> that is down to an integer.
> With Math.round ir rounds off the number to the nearest integer.
>
> If you run the code I have modified from yours, you will see what I mean. You
> will notice that rndV1 always stays at 0 yet rndV2 will deviate from 0 to 1.
> Making it a more accurate way of finding a true random number.
>
> var rnd:Number = Math.random();
> var rndV1 = Math.floor(rnd);// Rounds ONLY down to the nearest integer
> var rndV2 = Math.round(rnd);// rounds up OR down to the nearest integer
>
> trace(rnd + "\t" + rndV1 + "\t" + rndV2);
>


Manno Bult
manno@xs4all.nl 
7. Re: How to make a random name appear
NSurveyor Oct 19, 2006 3:42 PM (in response to anubia)DazFaz you've got it all backwards! Math.floor() WILL give you "true" random, while Math.round() won't!
http://www.quip.net/blog/2006/flash/choosingrandomnumbersredux 
8. Re: How to make a random name appear
NSurveyor Oct 19, 2006 3:47 PM (in response to NSurveyor)To further demonstrate this: 
9. Re: How to make a random name appear
Newsgroup_User Oct 20, 2006 12:58 AM (in response to anubia)
Ah!, that was the thing I was afraid of I had overlooked. Thanks.
Manno
NSurveyor wrote:
> DazFaz you've got it all backwards! Math.floor() WILL give you "true" random, while Math.round() won't!
>
> http://www.quip.net/blog/2006/flash/choosingrandomnumbersredux


Manno Bult
manno@xs4all.nl 
10. Re: How to make a random name appear
DazFaz Oct 20, 2006 2:59 AM (in response to NSurveyor)Now I’ve got my teeth into this problem, and to prove my initial deposition, does anyone know how the Math.random() function works out its calculations? I have made a few calculations but before I can quantify these I need a better understanding of how this function comes to its final number. And more importantly to how many decimal places does it work to. Oh and why that many too. 
11. Re: How to make a random name appear
Newsgroup_User Oct 20, 2006 3:30 AM (in response to NSurveyor)
You'd have to find someone more harcore than me to get an anwer to your
first question... Although NS' explanation has to do with rounding by
floor, round or ceil and not so much with the random function. That is
used in all mentioned scripts.
And why does Math.random() yeild so much decimals? Dunno, how many bits
would it take to store such a number?
But why does it change the number of decimals:
0.976767854765058 0 1
0.88760749110952 0 1
above traces come from code below:
var arrayLength:Number = 1;
var rnd:Number = Math.random() * arrayLength;
var rndV1 = Math.floor(rnd);
var rndV2 = Math.round(rnd);
trace(rnd + "\t" + rndV1 + "\t" + rndV2);
Nice puzzle for the weekend :) (gotta get back to work...)
DazFaz wrote:
> Now I?ve got my teeth into this problem, and to prove my initial deposition,
> does anyone know how the Math.random() function works out its calculations? I
> have made a few calculations but before I can quantify these I need a better
> understanding of how this function comes to its final number. And more
> importantly to how many decimal places does it work to. Oh and why that many
> too.
>


Manno Bult
manno@xs4all.nl 
12. Re: How to make a random name appear
qootz Oct 20, 2006 4:25 AM (in response to anubia)thanks for all