Created
May 14, 2019 18:59
-
-
Save jaspertravers/abafb5c63397985e65eff20f020e24d6 to your computer and use it in GitHub Desktop.
Find srand seed to output a short message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <string.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| int SIZE = 27; | |
| int * msg_to_code (char msg[], int length) { | |
| int * code = malloc (sizeof (int) * length); | |
| for (int index = 0; index < length; index++) { | |
| if (msg[index] == ' ') { | |
| code[index] = 27; | |
| continue; | |
| } | |
| code [index] = msg [index] - 'a'; | |
| } | |
| return code; | |
| } | |
| int match (int * code, int length) { | |
| int seed = 0; | |
| int element = 0; | |
| int get; | |
| while (1) { //while true | |
| srand (seed); //set seed | |
| while (element < length) { | |
| get = rand() % SIZE; //get rand num within our range | |
| if (code[element] != get) { // if no match, go to next loop | |
| break; | |
| } | |
| element++; | |
| } | |
| if (element == length) { // if we match all elements, return seed | |
| return seed; | |
| } | |
| element = 0; //reset inner counter | |
| seed++; //increment outer counter | |
| if (seed % 1000000 == 0) { //progress monitor | |
| printf ("%d\n", seed); | |
| } | |
| } | |
| return seed; | |
| } | |
| void test (int seed, int length) { | |
| int get; | |
| char out[length]; | |
| srand (seed); | |
| for (int index = 0; index < length; index++) { | |
| get = rand() % SIZE + 'a'; | |
| out[index] = get; | |
| printf ("v: %c\n", get); | |
| } | |
| //printf ("test: %s\n", out); //this is printing wack characters... | |
| return; | |
| } | |
| int main ( int argc, char** argv ) { | |
| int * code; | |
| char * msg; | |
| int length; | |
| int seed; | |
| printf ("input: %s\n", argv[1]); | |
| //msg = "ariel"; | |
| msg = argv[1]; | |
| length = strlen (msg); | |
| code = msg_to_code(msg, length); | |
| seed = match (code, length); | |
| printf ("seed: %d\n", seed); | |
| test (seed, length); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment