After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 768345 - Generator stuck in a loop
Generator stuck in a loop
Status: RESOLVED FIXED
Product: gnome-sudoku
Classification: Applications
Component: general
3.18.x
Other Linux
: Normal normal
: ---
Assigned To: gnome-sudoku-maint
gnome-sudoku-maint
: 768751 769758 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2016-07-03 17:56 UTC by Yuri Khan
Modified: 2016-08-13 08:08 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Seed the random number generator before generating each puzzle (782 bytes, patch)
2016-07-07 16:19 UTC, Yuri Khan
none Details | Review
Seed the random number generator once on start (2.47 KB, patch)
2016-07-07 17:38 UTC, Yuri Khan
reviewed Details | Review
qqwing-wrapper: seed RNG (1.12 KB, patch)
2016-07-12 04:05 UTC, Michael Catanzaro
committed Details | Review
Switch to C++ 11 (645 bytes, patch)
2016-07-12 04:06 UTC, Michael Catanzaro
committed Details | Review
qqwing-wrapper: Drop using statements (3.91 KB, patch)
2016-07-12 04:06 UTC, Michael Catanzaro
committed Details | Review
qqwing-wrapper: Seed RNG (1.06 KB, patch)
2016-07-12 04:16 UTC, Michael Catanzaro
committed Details | Review
Fixup for the previous commit (851 bytes, patch)
2016-07-12 04:23 UTC, Michael Catanzaro
committed Details | Review

Description Yuri Khan 2016-07-03 17:56:35 UTC
I’m getting the same puzzles over and over.

I start gnome-sudoku and click “Very Hard”. Expected behavior: I get a new puzzle that I have never seen before. Observed behavior: I get the same puzzle each time I start the program.

    ..8 ..7 1..
    ... 8.. .4.
    5.6 1.. 3..

    ..3 ..4 ..9
    7.. ... ..3
    9.. 7.. 4..

    ..1 ..2 6.5
    .6. ..8 ...
    ..2 3.. 8..

I click “New Puzzle”, “Very Hard”. Observed behavior: I get a different puzzle, again, it’s always the same.

    ... .9. ...
    .1. 8.3 .4.
    6.. ... ..2

    .9. .5. .3.
    5.4 ... 7.9
    ..2 .4. 1..

    ..9 .8. 4..
    .4. ... .6.
    3.. 5.4 ..7

I have not tried reproducing the problem on a different user profile.

What information do I need to provide so that this could be investigated? Alternatively, how can I get the generator unstuck?
Comment 1 Yuri Khan 2016-07-07 16:17:27 UTC
Oh what the hell. I looked at the source for qqwing.

It uses the C runtime library pseudo-random number generator, rand(). It expects the client code to seed that. The command-line sudoku generator in main.cpp does that. gnome-sudoku does not.

Additionally, gnome-sudoku keeps a directory of all puzzles ever solved and has a function to determine if a particular puzzle is contained in that directory (SudokuBoard.is_finished), but this function is never called since the removal of SudokuStore in 0e364d4 (“Remove SudokuStore and the puzzle data files” by Parin Porecha on 2014-08-06).

A simple, albeit not quite correct, workaround would be to seed the generator in lib/qqwing-wrapper.cpp, function qqwing_generate_puzzle.

As before, I don’t have a GNOME 3.21 development environment handy, but the patch that will follow fixes the problem for me on 3.18.2 as packaged by Ubuntu.
Comment 2 Yuri Khan 2016-07-07 16:19:31 UTC
Created attachment 331005 [details] [review]
Seed the random number generator before generating each puzzle

Rebased to git://git.gnome.org/gnome-sudoku:master as of 2016-06-26 (commit 283f24c).
Comment 3 Yuri Khan 2016-07-07 16:47:56 UTC
Hmm, scratch that. Seeding the generator with current time leads to duplicates if several puzzles are generated in quick succession, which is what happens when printing multiple puzzles.
Comment 4 Yuri Khan 2016-07-07 17:38:25 UTC
Created attachment 331009 [details] [review]
Seed the random number generator once on start

This is a bit more invasive. I seed the random generator in the only C++-based module in this program, expose that in the QQwing wrapper class, and that through the SudokuGenerator class all the way up to Sudoku.main.

I tried putting the call to QQwing.seed in the SudokuGenerator’s initializer function, class constructor, or static constructor. It doesn’t help, because that class is never constructed but only used for its static functions.

I do not understand vala well enough to make the QQwing namespace available to the main class; nor is it necessarily a good idea.
Comment 5 Michael Catanzaro 2016-07-09 17:29:12 UTC
Wow. How did we not notice this.
Comment 6 Michael Catanzaro 2016-07-12 04:05:35 UTC
Review of attachment 331009 [details] [review]:

Thanks for this patch. I'm going to go with a simpler approach that doesn't require punching through so many layers and modifying the vala files, but your help with this was much appreciated.
Comment 7 Michael Catanzaro 2016-07-12 04:05:56 UTC
The following fixes have been pushed:
7d404be qqwing-wrapper: seed RNG
8ba9104 Switch to C++ 11
7b237c6 qqwing-wrapper: Drop using statements
Comment 8 Michael Catanzaro 2016-07-12 04:05:59 UTC
Created attachment 331294 [details] [review]
qqwing-wrapper: seed RNG

So we don't repeat same puzzles each time Sudoku is started.

Credit to Yuri Khan for discovering and debugging this issue (and for
submitting a different fix).
Comment 9 Michael Catanzaro 2016-07-12 04:06:03 UTC
Created attachment 331295 [details] [review]
Switch to C++ 11

We could go all the way to C++ 14, but we don't need it for anything. I
want C++ 11 to use std::call_once.
Comment 10 Michael Catanzaro 2016-07-12 04:06:07 UTC
Created attachment 331296 [details] [review]
qqwing-wrapper: Drop using statements

These make the code harder to read.
Comment 11 Michael Catanzaro 2016-07-12 04:16:31 UTC
The following fix has been pushed:
30065ef qqwing-wrapper: Seed RNG
Comment 12 Michael Catanzaro 2016-07-12 04:16:34 UTC
Created attachment 331297 [details] [review]
qqwing-wrapper: Seed RNG

This is ugly. On the master branch, we will use std::once_flag, but for
gnome-3-20 let's avoid adding a dependency on C++ 11.
Comment 13 Michael Catanzaro 2016-07-12 04:23:16 UTC
The following fix has been pushed:
672deab Fixup for the previous commit
Comment 14 Michael Catanzaro 2016-07-12 04:23:19 UTC
Created attachment 331298 [details] [review]
Fixup for the previous commit

once_flag needs to be static or this gets called multiple times....
Comment 15 Michael Catanzaro 2016-07-13 02:05:06 UTC
*** Bug 768751 has been marked as a duplicate of this bug. ***
Comment 16 Michael Catanzaro 2016-08-13 08:08:25 UTC
*** Bug 769758 has been marked as a duplicate of this bug. ***