[Haskell-beginners] slightly scramble list randomly

Brent Yorgey byorgey at seas.upenn.edu
Sat Oct 22 22:43:34 CEST 2011


One approach you might consider is randomly generating a sequence of
N*M or so swaps (i.e. generate an index i between 0 and (N-2), and
swap the elements at positions i and i+1).  Of course, this way it is
possible to end up with an element very far away from its original
position, but with only a very small probability.  "On average" I
would expect things to end up just moved around a bit like you want.

-Brent

On Sat, Oct 22, 2011 at 10:50:29AM -0700, Dennis Raddle wrote:
> I want to take a list of size N that starts in ascending order and
> "slightly randomly scramble it"--- each element is moved within M
> steps of its original position (there can be some tolerance for
> slightly over M).
> 
> The application is a backtracking algorithm that produces random
> solutions but tends to favor certain choices--at each node of the
> search tree I can evaluate the goodness of each possible next step,
> but I don't want the algorithm to always take the best choice. Just
> roughly, much of the time.
> 
> Dennis
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



More information about the Beginners mailing list