Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Homeworks / homework1.cpp
diff options
context:
space:
mode:
Commit3c9dafd3d2977d4ac922886b0b00b566cd5288c1 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-11-24 17:26:29 +0200
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-11-24 17:26:29 +0200
Files8589284efc92cb825a166980b18964d10bac8a5e /Year 1/Programming introduction/Homeworks/homework1.cpp
uni-projects-3c9dafd3d2977d4ac922886b0b00b566cd5288c1.tar
uni-projects-3c9dafd3d2977d4ac922886b0b00b566cd5288c1.tar.gz
uni-projects-3c9dafd3d2977d4ac922886b0b00b566cd5288c1.zip
Parent0139d20b8844b4f1946c84b07e567faf5ba618c7 (diff)
Fixed and improved secret generation
1 files changed, 9 insertions, 8 deletions
-rw-r--r--Year 1/Programming introduction/Homeworks/homework1.cpp17
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;