Talk:Find the missing permutation: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Prototype Tcl Solution: I hadn't tried any of the Tcl tasks, myself.)
(→‎Perl shuffle: mersenne twister.)
Line 19: Line 19:
:::: Yeah, [http://www.jstatsoft.org/v11/i01/paper this paper (see page 48)] suggests that <code>perl</code>'s <code>rand</code> isn't guaranteed to be much good. —[[User:Underscore|Underscore]] ([[User talk:Underscore|Talk]]) 17:15, 24 December 2009 (UTC)
:::: Yeah, [http://www.jstatsoft.org/v11/i01/paper this paper (see page 48)] suggests that <code>perl</code>'s <code>rand</code> isn't guaranteed to be much good. —[[User:Underscore|Underscore]] ([[User talk:Underscore|Talk]]) 17:15, 24 December 2009 (UTC)
::::: The table on p.53 shows about what I'd expect; the standard rand() is not suitable for either crypto or (proper) Monte Carlo use, and this is normal for a general use RNG in any language. But for shuffling a short list of permutations for a RC task, well, it's just fine. The mixing up is just to make the answer not blindingly obvious after all. –[[User:Dkf|Donal Fellows]] 19:00, 24 December 2009 (UTC)
::::: The table on p.53 shows about what I'd expect; the standard rand() is not suitable for either crypto or (proper) Monte Carlo use, and this is normal for a general use RNG in any language. But for shuffling a short list of permutations for a RC task, well, it's just fine. The mixing up is just to make the answer not blindingly obvious after all. –[[User:Dkf|Donal Fellows]] 19:00, 24 December 2009 (UTC)
:::::: Python uses [[wp:Mersenne twister]] as its PRNG which is supposed to be of high quality. There is a Perl implementation [http://search.cpan.org/search?module=Math%3A%3ARandom%3A%3AMT%3A%3AAuto om CPAN]. --[[User:Paddy3118|Paddy3118]] 11:10, 25 December 2009 (UTC)
::::: BTW, it might be worth seeing if there's any ideas for tasks that can be harvested from that paper. After all, it's always good to encourage less-experienced programmers to understand the limits of the PRNGs they're using. –[[User:Dkf|Donal Fellows]] 19:03, 24 December 2009 (UTC)
::::: BTW, it might be worth seeing if there's any ideas for tasks that can be harvested from that paper. After all, it's always good to encourage less-experienced programmers to understand the limits of the PRNGs they're using. –[[User:Dkf|Donal Fellows]] 19:03, 24 December 2009 (UTC)



Revision as of 11:10, 25 December 2009

Perl shuffle

An odd observation...I used a Perl script derived from the Knuth shuffle task to shuffle an ordered list of permutations. I find it interesting that the left two columns show a greater vertical repeat frequency and length than the right two columns. --Michael Mol 06:32, 24 December 2009 (UTC)

Looking at it, I suspect that the Perl impl of that task has a bug in it; it doesn't appear to always guarantee to consider shuffling the first element. (I think. I might have also misread it.) For the Tcl version, what I did was do some frequency analysis to check whether the shuffle was fair; we shuffled the list 1,2,3,4,5 a hundred thousand times and counted up the total for each position in the list across all the runs; when the total for each column was close to 300k, we had a reasonable estimate that there weren't any subtle errors. (We checked for gross errors by eyeballing it.) My perl is very rusty though, so I'm not quite sure how to write the same thing. Perhaps later...
Do you want me to add in the Tcl solution for this task? –Donal Fellows 09:41, 24 December 2009 (UTC)

Hmm, when I test the Perl shuffle code with: <lang perl>my @tot = (0, 0, 0, 0, 0); for my $x (1 .. 100000) {

 my @a = shuffle(1, 2, 3, 4, 5);
 for (0 .. 4) {
   $tot[$_] += $a[$_];
 }

} print "totals: @tot\n"</lang> I get totals that indicate that things are OK. Not sure what's wrong then, if anything. Don't think it really matters though; the list of permutations doesn't have an obviously missing element, so a short program is indeed the best way to find the missing item. –Donal Fellows 10:31, 24 December 2009 (UTC)

I wrote that function. I based it closely on the Wikipedia pseudocode, and I was especially careful to avoid fencepost errors. I'm pretty sure it's correct. —Underscore (Talk) 12:56, 24 December 2009 (UTC)
I suspect that it was just someone forgetting that every permutation should be equally likely, even the “unlikely” ones. The testing code indicates fairness (or balanced wrongness of course, but I can't conceive of that code doing it). –Donal Fellows 13:15, 24 December 2009 (UTC)
Heh. I didn't try running several times, so that one result set definitely isn't a representative sample. However I do use that Perl script for a few other things at home (It's great for shuffling things before they get to xargs when doing find-based playlists). I tried adding a reverse and reshuffle stage (the Knuth shuffle itself sits in its own function), and had elements pop up near the beginning of the list I'd forgotten I'd even had. That suggests to me there's something broken about my Perl implementation's rand function. As the Wp artical mentions, your shuffle quality is limited by your source of random numbers. I'll write a program some time today to do a more representative search and check of the results I noticed. --Michael Mol 16:05, 24 December 2009 (UTC)
Yeah, this paper (see page 48) suggests that perl's rand isn't guaranteed to be much good. —Underscore (Talk) 17:15, 24 December 2009 (UTC)
The table on p.53 shows about what I'd expect; the standard rand() is not suitable for either crypto or (proper) Monte Carlo use, and this is normal for a general use RNG in any language. But for shuffling a short list of permutations for a RC task, well, it's just fine. The mixing up is just to make the answer not blindingly obvious after all. –Donal Fellows 19:00, 24 December 2009 (UTC)
Python uses wp:Mersenne twister as its PRNG which is supposed to be of high quality. There is a Perl implementation om CPAN. --Paddy3118 11:10, 25 December 2009 (UTC)
BTW, it might be worth seeing if there's any ideas for tasks that can be harvested from that paper. After all, it's always good to encourage less-experienced programmers to understand the limits of the PRNGs they're using. –Donal Fellows 19:03, 24 December 2009 (UTC)

Prototype Tcl Solution

=={{header|Tcl}}==
{{libheader|tcllib}}
<lang tcl>package require struct::list package require struct::set

  1. Make complete list of permutations of a string of characters

proc allCharPerms s {

   set perms {}
   set p [struct::list firstperm [split $s {}]]
   while {$p ne ""} {

lappend perms [join $p {}] set p [struct::list nextperm $p]

   }
   return $perms

}

  1. The set of provided permutations (wrapped for convenience)

set have {

   ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC BCAD CADB CDBA CBAD ABDC
   ADBC BDCA DCBA BACD BADC BDAC CBDA DBCA DCAB

}

  1. Get the real list of permutations...

set all [allCharPerms [lindex $have 0]]

  1. Find the missing one(s)!

set missing [struct::set difference $all $have] puts "missing permutation(s): $missing"</lang>

Which version of Tcl does that require? struct::list Doesn't seem to come with Ubuntu 9.10's Tcl v 8.5:
shortcircuit@dodo~
11:27:02 $ tclsh8.
tclsh8.4  tclsh8.5  
shortcircuit@dodo~
11:27:02 $ tclsh8.5 missing.tcl 
can't find package struct::list
    while executing
"package require struct::list"
    (file "missing.tcl" line 1)
shortcircuit@dodo~
11:27:11 $

--Michael Mol 16:30, 24 December 2009 (UTC)

Requires a version of tcllib (which I did note; just marked nowiki to stop this talk page from going into the system) which it looks like Ubuntu (at the very least) doesn't install by default. It's been part of the overall tcllib system since at least tcllib 1.4 (that's the earliest version tag I can find in the CVS repo) which was from 2003. (I don't list tcllib itself in the script because that'd be a nonsense, analogous to – and nearly as silly as – requiring the whole of CPAN in one program...)
Not only did it not install it by default, it wasn't in the "Suggest" or "Recommend" section for the tcl8.4 and tcl8.5 packages, which was why I couldn't find it. (A bug report should probably be filed for that.) --Michael Mol 00:12, 25 December 2009 (UTC)
I don't know who's the maintainer of the Ubuntu (or Debian) package for tcllib (or even Tcl for that matter). –Donal Fellows 08:51, 25 December 2009 (UTC)
I'll also note that I've been using tcllib in quite a few tasks, and I'm actually not a heavy user of it by Tcl community standards. I guess my user profile is a bit odd. ;-) –Donal Fellows 09:00, 25 December 2009 (UTC)
I hadn't tried running any until now, I suppose. :) I just wanted to see which permutation it identified as missing. When I didn't see much under the "recommended" section for the tcl packages, I presumed that the standard library was tied in. --Michael Mol 09:58, 25 December 2009 (UTC)

Any notable problems with this task?

If nothing's been found particularly wrong with this task by Sunday night, I'll switch it over as a full task. --Michael Mol 19:01, 24 December 2009 (UTC)

It looks good to go now. It's clear what we have to do, and it's nicely solved with a permutation engine and some set arithmetic. –Donal Fellows 19:13, 24 December 2009 (UTC)