random string

Reply

Join Date: Sep 2006
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

random string

 
0
  #1
Oct 28th, 2007
I have small question:
How can I create a random string from 'a' to 'z'?
thank you!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: random string

 
0
  #2
Oct 28th, 2007
Create a string containing all the letters you want to use. Seed and use rand() to access elements (using the % operator to force the numbers within the string's range). Strcat in a loop. Pseudo-code.

  1. my_letters = "abcdefghijklmnopqrstuvwxyz" /* allowable letters for your random string */
  2. my_random_string /* your random string. Initially empty */
  3.  
  4. srand( time(0) ) /* seed rand */
  5.  
  6. for i=0 to 10 /* or however long you want it */
  7. append( my_random_string, /* append to this string */
  8. my_letters[rand()%strlen(my_random_string)] ) /* this letter */
  9. end loop
Or something like that. Alternatively you could just rand() (limit up to 25) and add 'a' onto the number and append that to your string. I think the above method is better though because you can extend the characters you use (you could put numbers, symbols, capital letters in it). That is harder to do with the second method I mentioned.
Last edited by twomers; Oct 28th, 2007 at 8:12 am.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: random string

 
0
  #3
Oct 28th, 2007
Use a loop to generate as many random numbers from 0 to 25 as you want and add the values to the string. When you get the random number, simply add 'a' to it and you will have all letters instead of 0-25
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

Re: random string

 
0
  #4
Oct 30th, 2007
Thank you for your helps
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC