Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random Characters???

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to generate a random character in a similar way it is to generate a random number using, say, the system clock, or would it be better to set up an array that holds all the characters in and work through that? I need to use single characters as a unique identifier you see.

Cheers
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could generate random integers that are between say 65 and 90 and then convert them to a character.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Bluesman:
Is it possible to generate a random character in a similar way it is to generate a random number using, say, the system clock, or would it be better to set up an array that holds all the characters in and work through that? I need to use single characters as a unique identifier you see.

Cheers


Either way sounds feasible. The algorithm you choose will depend on the requirements and what kind of "randomness" you desire. Do you need to avoid duplicates? If so, then you probably want to use a List of available chars and remove the ones that have been used. This is a fairly common way to pick anything at random without duplication.

On the other hand, if you don't care about duplicates, generating a random number between 'a' and 'z'. Since characters are really numerical values that use the Unicode character set to translate into a char, you can generate numbers between the Unicode values of 'a' and 'z'. So you might want to start this approach by writing a small methd that returns a random number between some min and max values that are passed in as parameters.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic