Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   displaying random characters (http://www.programmingforums.org/showthread.php?t=2491)

chepfaust Feb 27th, 2005 2:15 AM

displaying random characters
 
ok here's something confusing the heck outta me. i'm making a random password generator of sorts, giving the user the option of numbers, lowercase letters, uppercase letters, or any combination thereof. there's more to the program than this obviously, but this is the relevant code i have set up to complete this task:

:

Function Random(Lowerbound As Long, Upperbound As Long)
Random = Int(Rnd * Upperbound) + Lowerbound
End Function

...

Let route = Random(1, 3)
            If route = 1 And chkuppercase.Value = 1 Then ' generate an uppercase letter
                For r = 1 To txtLen.Text
                    Let m = Random(65, 90)
                    output = output & Chr(m)
                Next r
            End If
            If route = 2 And chklowercase.Value = 1 Then ' generate a lowercase letter
                For r = 1 To txtLen.Text
                    Let n = Random(97, 122)
                    output = output & Chr(n)
                Next r
            End If
            If route = 3 And chknumber.Value = 1 Then ' generate a number
                For r = 1 To txtLen.Text
                    output = output & Random(0, 9)
                Next r
            End If


i don't know if anyone can understand what exactly i've done with the code or the role of all the variables, but allow me to tell you what happens as a result of execution:

1. a random number within the respective ASCII range (uppercase 65-90, lowercase 97-122) is stored in variable m or n. m corresponds to uppercase letters, n with lowercase letters.
2. a chr(m) and chr(n) statement (is supposed to) append the corresponding character or number to string 'output.'
3. string 'output' ends up consisting of a bunch of gibberish hash marks and non-alphanumeric characters. sometimes i do get actual numbers or text, but not often.

so, any ideas as to why i'm not getting actual alpha/numeric output all the time? all i could find in my own research was somebody using a non-system font having this problem, but i tried terminal, times new roman, lucida console, ms sans serif; every default font i could think of but it didn't make a difference.

i suspect there may be a problem with my randomize function...?

thanks

Ooble Feb 27th, 2005 5:45 AM

Your random-number function is a little dodgy - it should do this:
:

Random = Int(Rnd * (Upperbound - Lowerbound)) + Lowerbound + 1

chepfaust Feb 27th, 2005 12:04 PM

i'll try that, thanks.

edit: ok, it DOES work now, and i've just realized that my code is utter crap and under perfect circumstances it wouldn't really make a random string anyway lol

back to the drawing board...

Rory Mar 2nd, 2005 5:31 AM

Don't forget to seed the generator:
randomize(timer)

Best way of generating passwords in my opinion is to have a biggish text file of words (you can get all sorts with a little googling), and shove them together with a few other characters so you get more memorable things like south27cow and hello@cheese etc.

Ooble Mar 2nd, 2005 5:19 PM

I take it you had a free Period 3?

chepfaust Mar 2nd, 2005 10:10 PM

Quote:

Originally Posted by Rory
Don't forget to seed the generator:
randomize(timer)

Best way of generating passwords in my opinion is to have a biggish text file of words (you can get all sorts with a little googling), and shove them together with a few other characters so you get more memorable things like south27cow and hello@cheese etc.

what does the "randomize(timer)" aspect do? i've just been using "randomize" to seed the generator, and it seems to work...

Ooble Mar 3rd, 2005 1:35 PM

It does exactly the same thing as just Randomize.

Rory Mar 10th, 2005 1:56 PM

What it does is.... random (sorry)

orez35 Nov 21st, 2006 3:39 AM

I need a program created to generate phone # lists to be imported into a fax broadcasting program. The area code and exchange #'s are known, but I need a program to generate all of the possible combinations for the last four digits.


All times are GMT -5. The time now is 11:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC