numbas / Numbas

A completely browser-based e-assessment/e-learning system, with an emphasis on mathematics
http://www.numbas.org.uk
Apache License 2.0
200 stars 118 forks source link

creating permutations #142

Closed BillFoster closed 12 years ago

BillFoster commented 12 years ago

I need permutations of [0,1,..,n-1]

So I tried to use the function, deal in math.js. First I had to include it in jme.js, along with randomint

new funcObj('randomint', [TNum], TNum, math.randomint); new funcObj('deal', [TNum] , TList , math.deal);

randomint works but not deal

I then rewrote it as

//returns a random number in range [0..N-1]
randomint: function(N) {
    return Math.floor(N*(Math.random()%1)); 
},

//a random shuffling of the numbers [0..N-1]
deal: function(N) 
{ 
    var J, K,L,  Q = new Array(N);
    for(var i=0;i<N;i++)
    {
        Q[i]=i;
    }
    for (J=0 ; J<N -1; J++)
        { K = J+math.randomint(N-J) ; L=Q[J] ; Q[J]= Q[K] ; Q[K] = L; }
    return Q; 
},

Still doesn't work.

If I ask for {deal(4)} I get [ , , , ]

Any obvious errors?

christianp commented 12 years ago

When you're constructing a TList, your list needs to contain TNum objects, not just normal javascript numbers. I've just committed a JME implementation of the deal function which does the conversion for you.

It would probably be good manners if the TList constructor did the conversion for you.

BillFoster commented 12 years ago

Thanks

-----Original Message----- From: Christian Perfect [mailto:reply+i-3374117- 16fe5c4925ff40455d76f208222804c7dc251af6-750780@reply.github.com] Sent: 24 February 2012 3:50 PM To: Bill Foster Subject: Re: [Numbas] creating permutations (#142)

When you're constructing a TList, your list needs to contain TNum objects, not just normal javascript numbers. I've just committed a JME implementation of the deal function which does the conversion for you.

It would probably be good manners if the TList constructor did the conversion for you.


Reply to this email directly or view it on GitHub: https://github.com/numbas/Numbas/issues/142#issuecomment-4159050