• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random Characters???

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could generate random integers that are between say 65 and 90 and then convert them to a character.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic