Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

If you have a finite number of puzzles, you can:

  • Build a list of puzzles, either all of them or some randomly picked;
  • Shuffle this list (see the Knuth Shuffle for instance);
  • Let your player play through this list;
  • When the list is empty, start with a new one.

EDIT

I didn't know this, but browsing SE made me realize that this is actually known as a "shuffle bag". Some more infos here, here or therethere.

EDIT 2

The classic Knuth Shuffle goes this way:

To shuffle an array a of n elements (indices 0..n-1):
    for i from n − 1 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]

Steven Stadnicki rightfully pointed out in his comment that this kind of thing doesn't prevent repetition on a reshuffle. A way to take this into account is to add a special case for the last item:

To reshuffle an array a of n elements and prevent repetitions (indices 0..n-1):
    return if n <= 2

    // Classic Knuth Shuffle for all items *except* the last one
    for i from n − 2 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]
        
    // Special case for the last item
    // Exchange it with an item which is *not* the first one
    r ← random integer with 1 ≤ r ≤ n - 1
    exchange a[r] and a[n - 1]

If you have a finite number of puzzles, you can:

  • Build a list of puzzles, either all of them or some randomly picked;
  • Shuffle this list (see the Knuth Shuffle for instance);
  • Let your player play through this list;
  • When the list is empty, start with a new one.

EDIT

I didn't know this, but browsing SE made me realize that this is actually known as a "shuffle bag". Some more infos here, here or there.

EDIT 2

The classic Knuth Shuffle goes this way:

To shuffle an array a of n elements (indices 0..n-1):
    for i from n − 1 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]

Steven Stadnicki rightfully pointed out in his comment that this kind of thing doesn't prevent repetition on a reshuffle. A way to take this into account is to add a special case for the last item:

To reshuffle an array a of n elements and prevent repetitions (indices 0..n-1):
    return if n <= 2

    // Classic Knuth Shuffle for all items *except* the last one
    for i from n − 2 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]
        
    // Special case for the last item
    // Exchange it with an item which is *not* the first one
    r ← random integer with 1 ≤ r ≤ n - 1
    exchange a[r] and a[n - 1]

If you have a finite number of puzzles, you can:

  • Build a list of puzzles, either all of them or some randomly picked;
  • Shuffle this list (see the Knuth Shuffle for instance);
  • Let your player play through this list;
  • When the list is empty, start with a new one.

EDIT

I didn't know this, but browsing SE made me realize that this is actually known as a "shuffle bag". Some more infos here, here or there.

EDIT 2

The classic Knuth Shuffle goes this way:

To shuffle an array a of n elements (indices 0..n-1):
    for i from n − 1 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]

Steven Stadnicki rightfully pointed out in his comment that this kind of thing doesn't prevent repetition on a reshuffle. A way to take this into account is to add a special case for the last item:

To reshuffle an array a of n elements and prevent repetitions (indices 0..n-1):
    return if n <= 2

    // Classic Knuth Shuffle for all items *except* the last one
    for i from n − 2 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]
        
    // Special case for the last item
    // Exchange it with an item which is *not* the first one
    r ← random integer with 1 ≤ r ≤ n - 1
    exchange a[r] and a[n - 1]
replaced http://gamedev.stackexchange.com/ with https://gamedev.stackexchange.com/
Source Link

If you have a finite number of puzzles, you can:

  • Build a list of puzzles, either all of them or some randomly picked;
  • Shuffle this list (see the Knuth Shuffle for instance);
  • Let your player play through this list;
  • When the list is empty, start with a new one.

EDIT

I didn't know this, but browsing SE made me realize that this is actually known as a "shuffle bag". Some more infos herehere, herehere or there.

EDIT 2

The classic Knuth Shuffle goes this way:

To shuffle an array a of n elements (indices 0..n-1):
    for i from n − 1 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]

Steven Stadnicki rightfully pointed out in his comment that this kind of thing doesn't prevent repetition on a reshuffle. A way to take this into account is to add a special case for the last item:

To reshuffle an array a of n elements and prevent repetitions (indices 0..n-1):
    return if n <= 2

    // Classic Knuth Shuffle for all items *except* the last one
    for i from n − 2 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]
        
    // Special case for the last item
    // Exchange it with an item which is *not* the first one
    r ← random integer with 1 ≤ r ≤ n - 1
    exchange a[r] and a[n - 1]

If you have a finite number of puzzles, you can:

  • Build a list of puzzles, either all of them or some randomly picked;
  • Shuffle this list (see the Knuth Shuffle for instance);
  • Let your player play through this list;
  • When the list is empty, start with a new one.

EDIT

I didn't know this, but browsing SE made me realize that this is actually known as a "shuffle bag". Some more infos here, here or there.

EDIT 2

The classic Knuth Shuffle goes this way:

To shuffle an array a of n elements (indices 0..n-1):
    for i from n − 1 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]

Steven Stadnicki rightfully pointed out in his comment that this kind of thing doesn't prevent repetition on a reshuffle. A way to take this into account is to add a special case for the last item:

To reshuffle an array a of n elements and prevent repetitions (indices 0..n-1):
    return if n <= 2

    // Classic Knuth Shuffle for all items *except* the last one
    for i from n − 2 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]
        
    // Special case for the last item
    // Exchange it with an item which is *not* the first one
    r ← random integer with 1 ≤ r ≤ n - 1
    exchange a[r] and a[n - 1]

If you have a finite number of puzzles, you can:

  • Build a list of puzzles, either all of them or some randomly picked;
  • Shuffle this list (see the Knuth Shuffle for instance);
  • Let your player play through this list;
  • When the list is empty, start with a new one.

EDIT

I didn't know this, but browsing SE made me realize that this is actually known as a "shuffle bag". Some more infos here, here or there.

EDIT 2

The classic Knuth Shuffle goes this way:

To shuffle an array a of n elements (indices 0..n-1):
    for i from n − 1 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]

Steven Stadnicki rightfully pointed out in his comment that this kind of thing doesn't prevent repetition on a reshuffle. A way to take this into account is to add a special case for the last item:

To reshuffle an array a of n elements and prevent repetitions (indices 0..n-1):
    return if n <= 2

    // Classic Knuth Shuffle for all items *except* the last one
    for i from n − 2 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]
        
    // Special case for the last item
    // Exchange it with an item which is *not* the first one
    r ← random integer with 1 ≤ r ≤ n - 1
    exchange a[r] and a[n - 1]
A solution that actually works.
Source Link
Laurent Couvidou
  • 9.2k
  • 2
  • 42
  • 58

If you have a finite number of puzzles, you can:

  • Build a list of puzzles, either all of them or some randomly picked;
  • Shuffle this list (see the Knuth Shuffle for instance);
  • Let your player play through this list;
  • When the list is empty, start with a new one.

EDIT

I didn't know this, but browsing SE made me realize that this is actually known as a "shuffle bag". Some more infos here, here or there.

EDIT 2

The classic Knuth Shuffle goes this way:

To shuffle an array a of n elements (indices 0..n-1):
    for i from n − 1 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]

Steven Stadnicki rightfully pointed out in his comment that this kind of thing doesn't prevent repetition on a reshuffle. A way to take this into account is to add a special case for the last item:

To reshuffle an array a of n elements and prevent repetitions (indices 0..n-1):
    return if n <= 2

    // Classic Knuth Shuffle for all items *except* the last one
    for i from n − 2 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]
        
    // Special case for the last item
    // Exchange it with an item which is *not* the first one
    r ← random integer with 1 ≤ r ≤ n - 1
    exchange a[r] and a[n - 1]

If you have a finite number of puzzles, you can:

  • Build a list of puzzles, either all of them or some randomly picked;
  • Shuffle this list (see the Knuth Shuffle for instance);
  • Let your player play through this list;
  • When the list is empty, start with a new one.

EDIT

I didn't know this, but browsing SE made me realize that this is actually known as a "shuffle bag". Some more infos here, here or there.

If you have a finite number of puzzles, you can:

  • Build a list of puzzles, either all of them or some randomly picked;
  • Shuffle this list (see the Knuth Shuffle for instance);
  • Let your player play through this list;
  • When the list is empty, start with a new one.

EDIT

I didn't know this, but browsing SE made me realize that this is actually known as a "shuffle bag". Some more infos here, here or there.

EDIT 2

The classic Knuth Shuffle goes this way:

To shuffle an array a of n elements (indices 0..n-1):
    for i from n − 1 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]

Steven Stadnicki rightfully pointed out in his comment that this kind of thing doesn't prevent repetition on a reshuffle. A way to take this into account is to add a special case for the last item:

To reshuffle an array a of n elements and prevent repetitions (indices 0..n-1):
    return if n <= 2

    // Classic Knuth Shuffle for all items *except* the last one
    for i from n − 2 down to 1 do
        j ← random integer with 0 ≤ j ≤ i
        exchange a[j] and a[i]
        
    // Special case for the last item
    // Exchange it with an item which is *not* the first one
    r ← random integer with 1 ≤ r ≤ n - 1
    exchange a[r] and a[n - 1]
Nope...
Source Link
Laurent Couvidou
  • 9.2k
  • 2
  • 42
  • 58
Loading
Added a simple solution to prevent repetitions
Source Link
Laurent Couvidou
  • 9.2k
  • 2
  • 42
  • 58
Loading
Added some references
Source Link
Laurent Couvidou
  • 9.2k
  • 2
  • 42
  • 58
Loading
Source Link
Laurent Couvidou
  • 9.2k
  • 2
  • 42
  • 58
Loading