Menu
Hi !
I am looking for an elegant way to mix arrays arrays. Could you give me one, please ?
Thanx for your help !
<i>
</i>array=[
['Bernard','Bernard'],
['Jean','Jean'],
['Jocelyne','Jocelyne'],
['Maud','Maud'],
['Rudolf','Rudolf']
]
function aleate()
{
return Math.pow(-1, Math.round(2*Math.random()))
}
alert(array.sort(aleate))
[code=php]
Array.prototype.rand=addRandFunction
function addRandFunction(){
var DaArray1=this
var DaArray2=new Array(DaArray1.length);for(var i=0;i<DaArray1.length;i++){DaArray2[i]=i}
var DaArray3=new Array(DaArray1.length);
for(i=0;i<DaArray1.length;i++){
var picked=Math.floor(Math.random()*DaArray2.length)
DaArray3[i]=DaArray1[DaArray2[picked]]
delete DaArray2[picked];
var New_Array=new Array()
var j=0;
for(var ii=0;ii<DaArray2.length;ii++){
if(typeof DaArray2[ii]!="undefined"){
New_Array[j]=DaArray2[ii];j++
}
}
DaArray2=New_Array
}
return DaArray3
}
alert([1,2,3].rand())
[/code]
<i>
</i><script>
var pos_ones=0
var neg_ones=0
for(i=0;i<10000;i++){
var rand0=Math.pow(-1, Math.floor(Math.random()*2) )
if(rand0==-1)neg_ones++
if(rand0==1)pos_ones++
}
alert("Math.floorn"+"positive onest"+(pos_ones/100)
+"%"+"nnegative onest"+(neg_ones/100)+"%")
pos_ones=0
neg_ones=0
for(i=0;i<10000;i++){
var rand1=Math.pow(-1, Math.round(1*Math.random()))
if(rand1==-1)neg_ones++
if(rand1==1)pos_ones++
}
alert("Math.roundn"+"positive onest"+(pos_ones/100)
+"%"+"nnegative onest"+(neg_ones/100)+"%")
</script>
[/size][i]Originally posted by Ultimater [/i]
[B]Hey Grand Mamamouch,
If randomizing all the element's postions in an array is
[b]not[/b] what you want us to post for you,
please be more specific. [/B][/QUOTE]
[code=php]
array=[
['Bernard','Bernard'],
['Jean','Jean'],
['Jocelyne','Jocelyne'],
['Maud','Maud'],
['Rudolf','Rudolf']
]
function aleate()
{
return Math.random()*2 -1
}
document.write(array.sort(aleate))
[/code]
[code=php]
function aleate()
{
return 2*Math.random() -1
}
[/code]
I'd have used:
Math.pow(-1, Math.floor(Math.random()*2))[/QUOTE]
<i>
</i>Math.round(Math.random())?-1:1
Hi all,
But still your chances of producing a[color=blue]-1[/color] or[color=blue]1[/color] didn't change.
Amazing :eek:
Usually Math.round tips-the-scale with Math.random like so:
[color=red] [size=+1]alert(Math.round(Math.random()*5))[/size] [/color]
Math.round rounds a random number from 0 to 5(excluding 0 and 5).
before round:after round
[color=red]0.0-0.49... :0[/color] (half change)
0.5-1.49... :1
1.5-2.49... :2
2.5-3.49... :3
3.5-4.49... :4
[color=red]4.5-5.00... :5[/color] (half chance)
[color=red] [size=+1]alert(Math.floor(Math.random()*6))[/size] [/color]
Math.floor [b]always[/b] rounds down and never up un-like Math.round.
Math.floor will floor a random number from 0 to 6(excluding 0 and 6).
before floor:after floor
0.0-0.9... :0
1.0-1.9... :1
2.0-2.9... :2
3.0-3.9... :3
4.0-4.9... :4
5.0-5.9... :5
(6 cannot be pick)[/QUOTE]
By the Way,
If you were wondering how I was checking the chances:[/quote]
My function
PHP Code:
function aleate()
{
return 2*Math.random() -1
}
in the end respects the inner arrays !
[/quote]
I think Grand Mamamouch wants to randomize the position of all the elements of an array's array(an array inside of an array).
If I understand correctly, he wants to turn:
[['Bernard','Bernard'],
['Jean','Jean'],
['Jocelyne','Jocelyne'],
['Maud','Maud'],
['Rudolf','Rudolf']]
into:
[['Jean','Bernard'],
['Jean','Rudolf'],
['Jocelyne','Maud'],
['Maud','Jocelyne'],
['Rudolf','Bernard']][/quote]
You really enjoy digging-up the trash, huh? [/quote]I was searching for a script that does this and when I saw your unusaul approach to gettin +/- 1 I thought I would comment.
Who cares which method you use to generate a random -1 or 1...[/quote]I may be wrong but I think it takes longer to execute Math.pow than to just subtract then multiply.
0.1.9 — BETA 5.16