random module? 
Author Message
 random module?

Python has a whrandom or something like that and I would like to create
   a random password generator at work in Ruby instead. The python goes
something like this:

import string
import whrandom

def generatePassword(minlen=5, maxlen=10):
        chars = string.letters + string.digits
        passwd = ""

        # determine password size
        passwd_size = whrandom.randint(minlen, maxlen)

        for x in range(passwd_size):
                # choose a random alpha-numeric character
                passwd += whrandom.choice(chars)

        return passwd

print generatePassword()



Mon, 01 Nov 2004 05:36:37 GMT  
 random module?

Quote:

> (It would actually be better to put chars in a constant somewhere,
> rather than re-generating the array each time.  And I would not be
> surprised if there were not a better way to generate it anyway.)

"not", not "not...not"

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Mon, 01 Nov 2004 06:36:02 GMT  
 random module?
Hello --

Quote:

> Python has a whrandom or something like that and I would like to create
>    a random password generator at work in Ruby instead.

Here's something that could get you started:

  def generate_password(minlen=5, maxlen=8)
    chars = ['A'..'Z','a'..'z',0..9].map { |a| a.to_a } .flatten
    len = minlen + rand(maxlen-minlen+1)
    (1..len).map { chars[rand(chars.size)] } .join
  end

(It would actually be better to put chars in a constant somewhere,
rather than re-generating the array each time.  And I would not be
surprised if there were not a better way to generate it anyway.)

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Mon, 01 Nov 2004 06:36:02 GMT  
 random module?

Quote:

> Python has a whrandom or something like that and I would like to create
>    a random password generator at work in Ruby instead. The Python goes
> something like this:

> import string
> import whrandom

> def generatePassword(minlen=5, maxlen=10):
>    chars = string.letters + string.digits passwd = ""

>    # determine password size
>    passwd_size = whrandom.randint(minlen, maxlen)

>    for x in range(passwd_size):
>            # choose a random alpha-numeric character passwd +=
>            whrandom.choice(chars)

>    return passwd

> print generatePassword()

well i'm not very good at ruby, but i was thinking of something like
-------------------------------------------------------------------
#!/usr/bin/ruby

def generatePassword(minlen = 5, maxlen = 10)

  passwd_len = (rand(maxlen - minlen) + minlen)
  passwd = ""

  while (passwd.length < passwd_len) do
    n = rand(74) + 48;
    passwd << n.chr if (n < 58 || n > 64 && n < 91 || n > 96)
  end

  password

end

puts generatePassword()
-----------------------------------------------
is there a more straight-forward way of doing it?

love,
pizza

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com



Mon, 01 Nov 2004 06:46:34 GMT  
 random module?
Hi,


Quote:
>"not", not "not...not"

But not is not in not...not, is it? '...' is exclusive.

Or as Yoda would say: Not not not every is.

Thorsten
--
Endorsing products is the American way of expressing individuality.
    - Calvin



Mon, 01 Nov 2004 06:46:29 GMT  
 random module?
Hi,


Quote:

>>"not", not "not...not"
>But not is not in not...not, is it? '...' is exclusive.

I'm sorry, I have not checked this not enough. The not might be not in
not...not, but not is. This should not have happened.

Thorsten
--
He who receives an idea from me, receives instruction himself without lessening
mine; as he who lights his taper at mine, receives light without darkening me.
        - Thomas Jefferson



Mon, 01 Nov 2004 07:07:19 GMT  
 random module?
Hi --

Quote:

> Hi,



> >>"not", not "not...not"
> >But not is not in not...not, is it? '...' is exclusive.
> I'm sorry, I have not checked this not enough. The not might be not in
> not...not, but not is. This should not have happened.

Wow, now we're really getting tied up in, ummm, nots.

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Mon, 01 Nov 2004 08:47:03 GMT  
 random module?


Quote:
> Python has a whrandom or something like that and I would like to create
>    a random password generator at work in Ruby instead. The Python goes
> something like this:

> import string
> import whrandom

> def generatePassword(minlen=5, maxlen=10):
>    chars = string.letters + string.digits
>    passwd = ""

>    # determine password size
>    passwd_size = whrandom.randint(minlen, maxlen)

>    for x in range(passwd_size):
>            # choose a random alpha-numeric character
>            passwd += whrandom.choice(chars)

>    return passwd

> print generatePassword()

def generatePassword(minlen=5, maxlen=10)
   chars = ("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a
   pwlen = rand(max - min) + min + 1
   (1..pwlen).to_a.collect {|x| chars[rand(chars.length)] }.join
end

puts generatePassword



Tue, 02 Nov 2004 00:27:48 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. random behavior in random module?

2. Random Module

3. Random Module

4. Random Module help

5. Random Numbers Module rv Release 1.1

6. module random: delay in "seeding"

7. Module for quasi-random sequences?

8. Bug in the standard module random ?

9. random rant on random files

10. A truly random $random??

11. ? generating random uniform and binomial random deviates for BIG integers

12. Unit tests and random.Random()

 

 
Powered by phpBB® Forum Software