LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2007, 11:49 PM   #1
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
random letters


I am playing with a script to generate random characters--ie UC or LC letters, numbers, etc.
After generating a random number (decimal), I use this statement to generate the character:
Code:
printf "\x$(printf "%x" $a)"
("a" is the decimal # already generated.)
This works perfectly--my question is" Is there a simpler way?

The best solution would be a direct way of generating random characters, but I have not seen such a thing.
 
Old 06-28-2007, 05:21 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
you could set an array of characters, then set a random number x within the range of 0 - number of array elements-1 and take the charArr[x] as random character.
 
Old 06-28-2007, 05:50 AM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
If want gibberish, then cat /dev/urandom or cat /dev/random will give you that (urandom being pseudo random, whereas random is more or less truly random, but is finite without external input - e.g. mouse movement etc.). Use strings to turn it into something a bit more recognisable:
Code:
strings /dev/urandom 
or 
strings /dev/random
edit: to test this, might be worth piping it into less, otherwise your terminal will very quickly fill up with rubbish!

Last edited by pwc101; 06-28-2007 at 05:51 AM.
 
Old 06-28-2007, 06:59 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,414

Rep: Reputation: 418Reputation: 418Reputation: 418Reputation: 418Reputation: 418
If it's passwords you want, why not use pwgen?
 
Old 06-28-2007, 08:19 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Original Poster
Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Thanks to all.....

The array method came to me while going to sleep--that one is the obvious head-slapper.

With the strings method, I don't see the easy way of limiting to a range---ie all LC letters. Also when I did "strings /dev/random" it printed maybe ten characters and then stopped.

No "pwgen" on this machine---will have to try that.
 
Old 06-28-2007, 08:24 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,414

Rep: Reputation: 418Reputation: 418Reputation: 418Reputation: 418Reputation: 418
pwgen should only be an apt-get away
 
Old 06-28-2007, 08:28 AM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by pixellany
Also when I did "strings /dev/random" it printed maybe ten characters and then stopped.
If you do "strings /dev/random", and then wiggle the mouse for a few seconds, it will create new random characters. man urandom has this to say on it:
Quote:
Originally Posted by man urandom
The /dev/random interface returns random bytes
only when sufficient amount of entropy has been collected.
If there is no entropy to produce the requested number of
bytes, /dev/random blocks until more entropy can be
obtained.

...

Bytes retrieved from /dev/random provide the highest quality
random numbers produced by the generator, and can be used to
generate long term keys and other high value keying
material.
If you only want lowercase/uppercase/whatevercase, perhaps a pipe to tr is your friend?

Last edited by pwc101; 06-28-2007 at 08:29 AM.
 
Old 06-28-2007, 08:55 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Original Poster
Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by pwc101
If you do "strings /dev/random", and then wiggle the mouse for a few seconds, it will create new random characters. man urandom has this to say on it:If you only want lowercase/uppercase/whatevercase, perhaps a pipe to tr is your friend?
Strings seems to be a really slow way of getting random letters, numerals, etc.

For setting the range, I think throwing away certain characters would skew the statistics. To generate random numbers, I first generate from 0 to an upper limit by using modulo division. Then I add a constant to set the lower end. This--in principle--gives a gaussian centered in the middle of the final range. I don't think you would get a gaussian distribution by just discarding.

This is really turning into an exercise in programming techniques---there is actually not a big market for random letters....
 
Old 06-29-2007, 02:56 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,494

Rep: Reputation: 235Reputation: 235Reputation: 235
perl -e 'print chr rand(127-32)+32 while 1'


Code:
@k$FX0wX#kM>:() gO1#gJpLdV8eWQRb5`r>
5=o]M34M71:i<a>1Iv|yxDw+?$\YnT1k
2fQjXzHN|z?}T"lXVT6D?sDsyN<Uk-~_kw
JUb0C&?R05>ur(MKm}HBrfj0V%>]<Ksf^'5Ll2H*
vW4A^+XKMOf~CvU&y\>*,Ve=MXc(pSR,r2C2w )z>n6&
+s;;{gpbrdB9(o8veUCX]Y]3fR S
 
Old 07-11-2007, 12:53 PM   #10
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Yes, an interesting programming exercise.

What is the goal? -- passwords?

What are the language constraints? -- bash only, or is perl allowed?
 
Old 07-11-2007, 04:36 PM   #11
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Definitely an interesting exercise in programming techniques.

What was the original goal? -- passwords?

What are the language constraints? -- bash only, or is perl allowed?
 
Old 07-11-2007, 05:45 PM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Original Poster
Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I wanted to get some random text in bash--it then turned into various bits of minutiae on how bash stores and converts raw bytes.

(I don't do Perl---I am learning Python, but that's another story.)
 
Old 07-12-2007, 02:26 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,494

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
I don't do Perl
you should try it, it's good for messing about with bytes...

e.g a bit of binary...

Code:
$  perl -e 'print unpack "B*", "Hello"'
0100100001100101011011000110110001101111

$ perl -e 'print pack "B*", "0100100001100101011011000110110001101111"'
Hello
 
Old 07-12-2007, 05:27 AM   #14
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
In post #1, where were you getting the decimal random #?

If you adopted the array method in post #2, how would you map the random # onto the array index? And I mean specific bash code -- i.e. will built-in bash arithmetic suffice, or will you need bc or something like it?

BTW, I agree:
Quote:
throwing away certain characters would skew the statistics
 
Old 07-12-2007, 08:04 AM   #15
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Original Poster
Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
bash has a built-in function: $RANDOM

I have not used the array method, but I assume that you would simply do:
num=$RANDOM
char=array[$num]

To avoid just throwing things away that don't fit the range (LO to HI), I set an upper bound which is the difference between HI and LO, then add LO back to the random number
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 06:48 PM
Broken Fedora Version? (National letters get messed up, random times) blackman890 Fedora 1 06-27-2005 07:51 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM
non english letters bynaar Slackware 8 01-21-2003 12:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration