Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-12-2005, 07:57 PM   #1
Registered: Jan 2005
Distribution: slackware
Posts: 83

Rep: Reputation: 15
Picking a character from a string randomly

Ok, if i have this code below, how do i pick a letter out of the string pass randomly?

var len = prompt("number of letters","")
var z = 0
while (z != len)
var pass="abcdefghijklmnopqrstuvwxyz1234567890"
var blank=''
var x = Math.floor(Math.random()*pass.length)

Old 08-12-2005, 08:13 PM   #2
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
Do you mean something like this?

var len = prompt("number of letters","");
var str = '';
var pass="abcdefghijklmnopqrstuvwxyz1234567890";
var passLen = pass.length - 1;
for( var z = 0; z < len; ++z )
   str += pass.charAt( Math.floor( Math.random() * passLen ) );
alert( 'Random string created is: ' + str );
Old 08-14-2005, 02:21 AM   #3
Registered: Jan 2005
Distribution: slackware
Posts: 83

Original Poster
Rep: Reputation: 15
yeah, thanks a lot, i actually ended up using substring and that made it easy for me, thanks.


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
python: converting a 3 character octal string into and ASCII char llama_meme Programming 1 07-06-2010 03:00 PM
Remove last character from file/string linuxchump Programming 34 06-08-2009 05:01 AM
how to print the first character in a string using strtok its_godzilla Programming 5 02-02-2005 11:22 AM
how to solve Character and String problems on linux?(screenshot) tradingbr Linux - General 0 04-29-2004 03:51 PM
Using sed to convert a string to a character? whansard Linux - General 2 01-10-2003 06:13 AM

All times are GMT -5. The time now is 12:06 PM.

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