Thread: random string with a set length...

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    random string with a set length...

    hey, I am just trying to generate a random string with a set length for a key to be later used to XOR "encrypt" some data. here is the code I am using now:

    Code:
    void RandCryptKey(char *szIn)
    {
    	int i;
    
    	srand(GetTickCount());
    
    	do i = rand();
    	while(i < 10000 && i > 1000);
    
    	wsprintf(szIn, "%d", i);
    }
    this function is being called in a loop by the way. now here is my problem, other than the fact that this is a rather inefficient method, I guess my code is looping so fast, that the same key is being generated for each call (I guess the seed is remaining the same). not to mention the fact that I am looking for a 4 digit number for the string, and the above code will still generate 5 digit numbers, even with my do while loop's conditional.

    GetTickCount is a Win32 API call to return the amount of time that the system has been running for in milliseconds by the way. and think of wsprintf as sprintf, it is simply the Win32 version since I am not including stdio.h.

    so as you see I am having quite the problems here, and I can't think of any other way to go about this.

    could anyone please help me out with an idea for an alternative method for doing this here please?

    any help/input is greatly appreciated. thank you in advance!
    Last edited by Bleech; 11-06-2006 at 05:15 PM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well call srand() exactly ONCE at the start of main.
    Calling it every time with a constant (which is what GetTickCount() will return in a short-lived program, no matter how many times you call it) will result in rand() also returning the same value.

    I think the FAQ explains this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    thanks Salem, I put the srand statement before the loop that calls this function and now it actually outputs random values .

    however it is still printing 5 digit numbers. you would think my do while's conditional would make sure that the number is less than 10000 and greater than 1000 (a 4 digit number) but it doesn't seem to have any relevance here .

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >you would think my do while's conditional would make sure that the number is less than 10000 and greater than 1000
    It's because your logic is backwards. Try:
    Code:
    	while (i < 1000 || i >= 10000);

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or you might prefer to write it like this:
    Code:
    	while (!(i < 10000 && i > 1000));

  6. #6
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    lol, yes swoopy, I came to that conclusion just before you posted.

    my solution was the first code replied with (and edited ):

    Code:
    while(i < 1000 && i >= 10000);
    however it was still printing 5 digit numbers I guess due to the nature of AND. using OR instead fixes the problem, it now prints only 4 digit numbers, thanks swoopy.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >(and edited
    Who me? Those rascals can be tricky.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM