4 Replies - 11135 Views - Last Post: 24 September 2012 - 03:50 PM

#1 novacrazy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 117
  • Joined: 01-March 11

Special random strings

Posted 29 March 2012 - 07:25 PM

I recently had to create an algorithm that could create a string of characters, that when XOR-ed with itself in reverse, produced no zeros, meaning none of the characters overlapped with each other when crossed with its reverse.
e.g. "D6C5E7CE8C"

Using your preference of random number generator, character sets, and whatever else you want really, not limiting by any library, try to make one that can top mine. Mine can do pow(2, 24) length string with that only guideline in about 2 seconds at most.

I'm very curious what you guys can come up with, because I tried like four different methods before I settled on this one. I'll post mine after a few other posts.

Is This A Good Question/Topic? 0
  • +

Replies To: Special random strings

#2 fireapple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-May 12

Re: Special random strings

Posted 26 May 2012 - 09:10 AM

This was actually pretty fun.
Without printing the output (since that's slower than the actual algorithm), using the *nix 'time' command, I found the program took on average 0.35 seconds to generate a string of length 2^24.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define CHAR_LIMIT 127 /* Corresponds to DEL. */

char generate_char(void)
{
    char value = rand() % CHAR_LIMIT;
    return (value < ' ') ? value + ' ' : value;
}

char* randstr(int length)
{
    char* string = (char*)calloc(length + 1, sizeof(char));
    char ch;
    int index = 0;

    while (index <= length / 2) 
        string[index++] = generate_char();
    while (index < length) {
        while ((ch = generate_char()) == string[length - index]);
        string[index++] = ch;
    }
    string[length] = 0;
    return string;
}

int main(int argc, char** argv) {
    if (argc != 2) {
        printf("usage: %s [strlen]\n", argv[0]);
        return EXIT_FAILURE;
    }
    srand(time(NULL));
    char* string = randstr(atoi(argv[1]));
    /* The code below is commented out so I could measure the time it takes
     * just to generate the string. Printing it obviously takes a long time.
     */
    /* puts(string); */
    free(string);
    return EXIT_SUCCESS;
}


Was This Post Helpful? 0
  • +
  • -

#3 novacrazy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 117
  • Joined: 01-March 11

Re: Special random strings

Posted 26 May 2012 - 09:44 AM

This is what I had come up with:

Spoiler


Which actually seems to run a lot better than it did when I made it originally...
Was This Post Helpful? 0
  • +
  • -

#4 fireapple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-May 12

Re: Special random strings

Posted 26 May 2012 - 10:03 AM

Oddly enough, I seem to be in the same situation. Guess it's running more quickly since I've let my computer cool down after all the extensive testing.
Screenshot here
Was This Post Helpful? 0
  • +
  • -

#5 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Special random strings

Posted 24 September 2012 - 03:50 PM

Here's the result of time for my code:

real 0m1.156s
user 0m1.109s
sys 0m0.077s

That's with optimization on. It improved slightly after I stuck a couple 'register' directives in there.

Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1