February 6th, 2003, 08:17 PM
Generate random 2K long strings
I need to feed a function with a 2 Kb long random string for testing purposes. I don't know how to generate this....
Has anyone of you an advise for me, plz?
February 7th, 2003, 02:12 AM
The concept's not that difficult. Think about it:
You have a fixed number of characters you want to use. For simplicity's sake, assign them to an array.
You know the size of this array, either through hard-coding, or by dynamically reading it's size.
char szChars = "ABCDEFGHIJKLMNO"; // etc...
Now, generate a random number between the bounds of this array, and pull the character from that index. Loop this 2000 times, and you have your random string.
What part specifically is troubling you?
Last edited by MJEggertson; February 7th, 2003 at 02:16 AM.
February 7th, 2003, 05:39 AM
Or you could do it without a character lookup table.
include time.h, stdlib.h
char random_string = "";
int i = 0;
srand (time (NULL));
while (i < sizeof (random_string))
random_string[i] = rand () % 255;
you get the idea :).
Last edited by xtorsyon; February 7th, 2003 at 05:49 AM.
February 7th, 2003, 06:28 AM
I understand very well your 2 examples and I thank you very very much.
What got me confused is the string declaration syntax (i have to admit my C-newbieness here :)
I thought it was
while you use
Are those equivalent?
Thank you since now.
February 7th, 2003, 08:05 AM
char *string and char string[N_CHAR] are two different things. char *string is not allocated and is not pointing to any array whereas char string[N_CHAR] is allocated N_CHAR bytes. Analyser gave an excellent explanation about pointers and arrays in here (http://forums.devshed.com/t51185/s.html). Hope this helps.