Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rnd function not so Random 1

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I have run into this issue before with VB6. RND seemingly follows the same ???? thing. it is not truly random.

I created a simple scrabble game for students at my school. but when I click the "new game" button the same letters come up every time. in the exact same order. clicking the "new game" button again does generate a different set of letters but for some reason the first time when the game is first started the exact same letters come up in the same order.
here are those 7 letters S N P H H U A.

Code:
    Dim RandomNumber As Integer
    Dim MyLetter, LetterToLoad As String
    For a = 1 To 7
        RandomNumber = Int((26 * Rnd) + 1)  'generate a random number from 1 to 26 letters
        MyLetter = Chr(RandomNumber + 64) 'returns the ASCII of a letter
        ' the letters are jgp images and right now are just worth 1 point , thus the name A1, B1, etc
        LetterToLoad = App.Path & "\Letters\" & Trim(MyLetter) & "1.jpg"
        ' load the images in the appropraiate images box at the bottom
        Me.imgNewLetters(a).Picture = LoadPicture(LetterToLoad)
    Next

Anyway to make it work like it should?
TIA

DougP
 

BTW, you do know that [tt]MyLetter[/tt] is NOT a String, it is Variant:
Code:
Dim RandomNumber As Integer
Dim [red]MyLetter[/red], LetterToLoad As String

Have fun.

---- Andy
 
This is kind of funny to see, considering it's been designed-in behavior of Microsoft Basics for a long, long time.

Just read the Rnd() and Randomize topics in the manual, which of course a properly installed legal copy of VB6 has online.

Avoid potentially misleading Visual Fred info. The real VB6 information is online yet, though be careful about broken links within the articles:




I'm not sure using a Variant there is a big deal, he assigns a Variant to it anyway:
Code:
MyLetter = Chr(RandomNumber + 64) 'returns the ASCII of a letter
 
Bob: I've only glanced at a few but they seem similar to what I would have expected. Is this info any different from what is in the help?

If so, can you tell me where his grandma lives in case I need some leverage in future? :)

Tony
 
DougP: working this way (needing to randomize to get "real" random numbers) is not as dumb as it might seem. For one thing, it means that you can compare a modded version of your program, with an original version which you know to be working correctly, to make sure they give the same answers. If it automatically randomized you could not do that.

Tony
 
Tony,

Bob's point is that Microsoft have been pulling and/or hiding VB6 info on their sites for a number of years in the forlorn hope that all the nasty VB programmers will move on to a nice new language instead.
 
Mike,

Thanks for clarifying that. I was just trying to clear up (without going through all the data to which he pointed) whether Bob's reference gave any info which was not already in the VB6 help.

However, I do take your point. Sometimes it seems like what you really need to get info you can actually use out of MS is by taking a course in arcane 101.

[whine]I like VB6. I understand it (for a given value of "understand"). I'd like to keep using it. Why are they so mean to me?[/whine]

Tony
 
I just noticed how truly awful that line of code I quoted above is.

Not only is the destination variable a Variant (ought to be String), and not only is the Variant versions of Chr() being called (ought to be Chr$), but the comment on that line states the inverse of what the line actually does.

Where is that YouTube video where they sing "Script Kiddie" sung to the tune of "Beach Baby?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top