From 3c9dafd3d2977d4ac922886b0b00b566cd5288c1 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 24 Nov 2021 17:26:29 +0200 Subject: Fixed and improved secret generation --- Year 1/Programming introduction/Homeworks/homework1.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'Year 1/Programming introduction/Homeworks/homework1.cpp') diff --git a/Year 1/Programming introduction/Homeworks/homework1.cpp b/Year 1/Programming introduction/Homeworks/homework1.cpp index c0542ec..d1ee749 100644 --- a/Year 1/Programming introduction/Homeworks/homework1.cpp +++ b/Year 1/Programming introduction/Homeworks/homework1.cpp @@ -221,18 +221,19 @@ int genSecretNum() srand (time(NULL)); int rnd = rand(), result = 0; + bool takenDigits[10] = { 0 }; + takenDigits[0] = true; // we can't have 0s in secret - for (int i = 0; i < NUMBER_LENGTH;) + for (int i = 0; i < NUMBER_LENGTH; i++) { - if (rnd % 10 != 0) + while (takenDigits[rnd % 10]) { - (result *= 10) += rnd % 10; - i++; + rnd /= 10; + if (rnd == 0) + rnd = rand(); } - else if (rnd == 0) - rnd = rand(); - - rnd /= 10; + result = result * 10 + rnd % 10; + takenDigits[rnd % 10] = true; } return result; -- cgit v1.3.1-23-gd82d