C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 6th, 2003, 08:17 PM
jakuza's Avatar
jakuza jakuza is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 65 jakuza User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 52 sec
Reputation Power: 11
Question 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

Reply With Quote
  #2  
Old February 7th, 2003, 02:12 AM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #3  
Old February 7th, 2003, 05:39 AM
xtorsyon xtorsyon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 3 xtorsyon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old February 7th, 2003, 06:28 AM
jakuza's Avatar
jakuza jakuza is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 65 jakuza User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 52 sec
Reputation Power: 11
Exclamation

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

Reply With Quote
  #5  
Old February 7th, 2003, 08:05 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,387 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 39 m 3 sec
Reputation Power: 4080
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Generate random 2K long strings

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap