/    Sign up×
Community /Pin to ProfileBookmark

An elegant way to mix arrays arrays…

Hi !

I am looking for an elegant way to mix arrays arrays. Could you give me one, please ?

Thanx for your help !

to post a comment
JavaScript

18 Comments(s)

Copy linkTweet thisAlerts:
@Willy_DuittMar 06.2005 — Like this??

<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))


.....Willy
Copy linkTweet thisAlerts:
@UltimaterMar 06.2005 — Hi all,

Hi Willy Duitt!

[img]http://boards1.wizards.com/images/smilies/dropjaw.gif[/img]

(This is a simily for some reason my IMG Code is off)

I cannot believe you randomized all the elements of that array

with so few line!

I made a function who's sole-purpose is to do what your function did

in 5 lines!
[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 even made a whole thread about it [url=http://www.webdeveloper.com/forum/showthread.php?s=&threadid=57731]someArray.rand()[/url]

Nextly I cannot believe you used Math.round and Math.random

together successfully without tilting-the-scale.

I think you got lucky because your statement with Math.pow is flexable,

you could have just as easily have used:

Math.pow(-1, Math.round([color=red]1[/color]*Math.random()))

I'd have used:

Math.pow(-1, Math.floor(Math.random()*2))

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)
Copy linkTweet thisAlerts:
@UltimaterMar 06.2005 — By the Way,

If you were wondering how I was checking the chances:

[size=-3]<i>
</i>&lt;script&gt;
var pos_ones=0
var neg_ones=0
for(i=0;i&lt;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&lt;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)+"%")
&lt;/script&gt;
[/size]
Copy linkTweet thisAlerts:
@Willy_DuittMar 06.2005 — I didn't write that...

I merely copied it from where the O/P cross-posted the same question on the PHP forum here: ?

http://www.webdeveloper.com/forum/showthread.php?s=&postid=329007#post329007

.....Willy
Copy linkTweet thisAlerts:
@UltimaterMar 06.2005 — 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.
Copy linkTweet thisAlerts:
@Willy_DuittMar 06.2005 — [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]


Seems as if noone actually understands the question...

I wonder if it's homework??

http://www.webxpertz.net/forums/showthread.php?t=32154

http://www.codingforums.com/showthread.php?t=53826

http://www.sitepoint.com/forums/showthread.php?t=240419

.....Willy
Copy linkTweet thisAlerts:
@JPnycMar 06.2005 — I don't think so. He's posted a fair amount here and none of the other questions seemed like homework assignments.
Copy linkTweet thisAlerts:
@Grand_MamamouchauthorMar 07.2005 — Hi all !

and thank you for your contributions !

A clever guy found a better-than-mine aleate fonction :

[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]


Stays the problem of the inner arrays broken. I want them intact, wich is not the case there. It's not a matter of presentation : i want to use the retrieved array as an array of intact inner arrays.

I would be fullfilled if you find a solution to this problem. It would be a great step for science ;-)
Copy linkTweet thisAlerts:
@Grand_MamamouchauthorMar 07.2005 — My function

[code=php]
function aleate()
{
return 2*Math.random() -1
}
[/code]


in the end respects the inner arrays !

Happy day !
Copy linkTweet thisAlerts:
@UltimaterMar 07.2005 — 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']]
Copy linkTweet thisAlerts:
@BigMoosieApr 13.2005 — I'd have used:

Math.pow(-1, Math.floor(Math.random()*2))[/QUOTE]


I'd have used:

2*(Math.round(Math.random())-0.5)
Copy linkTweet thisAlerts:
@UltimaterApr 13.2005 — You really enjoy digging-up the trash, huh? I'm sure that the host's problem has long-long resolved...

Also you can always do this one:
<i>
</i>Math.round(Math.random())?-1:1


Who cares which method you use to generate a random -1 or 1...

The only valuable difference would be the speed difference, which in that case

would totally defeat the purpose of using the sort

function all-together... For a faster way to randomize all the elements of an array, you'd be able to beat the speed of the sort function by almost 10x with almost any custom-made function...

And I don't feel like finding the link of any such function because the host of this thread has probably long resolved his/her problem.
Copy linkTweet thisAlerts:
@7studApr 13.2005 — 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]

If you truly understood what you posted, there would be no need to run a simulation. The probablility distribution is in fact skewed, you just didn't check for all the outcomes, and all the outcomes are used by sort().
Copy linkTweet thisAlerts:
@7studApr 13.2005 — 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]

Do you really? To me it's quite obvious that is not what they want. The op said they wanted to respect the inner arrays, and if you take a few secs to run their example, you could see it does just that. Since they said the example does what they want, you are mistaken.
Copy linkTweet thisAlerts:
@BigMoosieApr 14.2005 — 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.
Copy linkTweet thisAlerts:
@UltimaterApr 14.2005 — If you want the fastest function to do this, I believe it is the following:

[url=http://www.webdeveloper.com/forum/showthread.php?s=&threadid=59096#post331175]http://www.webdeveloper.com/forum/showthread.php?s=&threadid=59096#post331175[/url]

You owe me one for finding that thread... it took 10 full-minutes of my time to find it...
Copy linkTweet thisAlerts:
@UltimaterApr 14.2005 — You might also be interested in this thread: [url=http://www.webdeveloper.com/forum/showthread.php?t=59517]useful tip: sort(function(){})[/url]
Copy linkTweet thisAlerts:
@BigMoosieApr 14.2005 — Thankyou
×

Success!

Help @Grand_Mamamouch 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.16,
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,
)...