LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 08-12-2005, 06:57 PM   #1
swatward
Member
 
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?

Code:
<script>
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)

z++
}
</script>
</body></html>
 
Old 08-12-2005, 07:13 PM   #2
leonscape
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?

Code:
<script>
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 );
</script>
 
Old 08-14-2005, 01:21 AM   #3
swatward
Member
 
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.
 
  


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


All times are GMT -5. The time now is 05:30 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration