The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Generate random 2K long strings
Discuss Generate random 2K long strings in the C Programming forum on Dev Shed. Generate random 2K long strings C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 6th, 2003, 08:17 PM
|
 |
Contributing User
|
|
Join Date: Oct 2002
Posts: 65
Time spent in forums: 46 m 52 sec
Reputation Power: 11
|
|
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?
Thx, bye
|

February 7th, 2003, 02:12 AM
|
|
Contributing User
|
|
Join Date: Jan 2002
Location: Seattle WA
Posts: 863
  
Time spent in forums: 22 sec
Reputation Power: 13
|
|
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.
Code:
char szChars[] = "ABCDEFGHIJKLMNO"; // etc...
You know the size of this array, either through hard-coding, or by dynamically reading it's size.
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
|
|
Junior Member
|
|
Join Date: Jan 2003
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Or you could do it without a character lookup table.
/*
include time.h, stdlib.h
*/
char random_string[2048] = "";
int i = 0;
srand (time (NULL));
while (i < sizeof (random_string))
{
random_string[i] = rand () % 255;
i++;
}
you get the idea  .
Last edited by xtorsyon : February 7th, 2003 at 05:49 AM.
|

February 7th, 2003, 06:28 AM
|
 |
Contributing User
|
|
Join Date: Oct 2002
Posts: 65
Time spent in forums: 46 m 52 sec
Reputation Power: 11
|
|
Quote: Originally posted by MJEggertson
What part specifically is troubling you? |
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
char *stringa;
while you use
char stringa[N_CHAR]
Are those equivalent?
Thank you since now.
Bye
|

February 7th, 2003, 08:05 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|