Make fill proportion a parameter to gen()

This commit is contained in:
2022-11-23 17:56:12 +00:00
parent 71db045c9f
commit 37e7c6481a
3 changed files with 11 additions and 11 deletions

9
sud.c
View File

@@ -22,9 +22,6 @@
#include <stdio.h>
#include <stdlib.h>
#define REM_PROB_NUMER 2
#define REM_PROB_DENOM 3
#define MAX_FILL_ATTEMPTS 32
static void initposs(struct sudoku *sud)
@@ -76,7 +73,7 @@ static void clear(struct sudoku *sud, unsigned r, unsigned c)
}
}
void gen(struct sudoku *sud)
void gen(struct sudoku *sud, double fill_prop)
{
/* Generate a completed sudoku. */
retry:
@@ -93,10 +90,10 @@ retry:
}
}
/* Remove cells with probability of `1 / REMOVE_PROB_RECIP`. */
/* Remove cells with probability of 1 - fill_prop. */
for (unsigned r = 0; r < NDIGITS; ++r) {
for (unsigned c = 0; c < NDIGITS; ++c) {
if (rand() % REM_PROB_DENOM < REM_PROB_NUMER)
if ((double)rand() / (double)RAND_MAX < 1 - fill_prop)
clear(sud, r, c);
}
}