Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   Complete Randomization (http://www.programmingforums.org/showthread.php?t=14523)

Megabyte Nov 19th, 2007 4:48 PM

Complete Randomization
 
Currently I'm working on a slot machine project purely for the fun and practice of it, and the fact that it isn't too hard and I'm relatively new still.

I'm using random numbers to determine what combination will result once it stops. However, each time I stop the program then run it again, I notice that it isn't totally random.

For example, the first time I run it, the random numbers generated may be 4, 27, 21, 5, etc ... And each corresponds to a certain set of pictures to load.
I close it, and the next time I run it, the same random numbers are generated, thus resulting in the same pictures. (It's not always loading 3 picture ones, but always 1, 2, 3 for the first time the pull button is clicked, 2, 2, and 1 the second time, etc.)

:

intChoose = Int((1 - 27 + 1) * Rnd + 27)

That's what I'm using to generate the numbers.
I thought generating a random numbers which would then generate another different random number would work, but again that does not solve the problem -- only increase possibilities.

Is there any way to completely randomize it so that it will choose a different random numbers each time the program is run, or is that a feature specific to VB6 (or most other code) that doesn't allow the programmer to change it?

Sane Nov 19th, 2007 4:55 PM

Re: Complete Randomization
 
You have to give the random number generator an arbitrary starting point. This is most often done by passing the computer's epoch to "seed the random number generator". Since the epoch you give it will be different each time you start the program, the generator will then start with a different set of random numbers.

Read Random Numbers In Visual Basic. More specifically, sections "Initializing the Random Numbers" and "Repeating Random Numbers".

Hope that helps.

grimpirate Nov 19th, 2007 4:58 PM

Re: Complete Randomization
 
I don't recall Visual Basic programming off the top of my head since I haven't done it in a while. However, I believe there was a Math class which generates random numbers. You should call this class and its method to generate a random number. What you have to check is in the documentation if the generator needs to be seeded with a value. The best way to do this is to seed the generator with the system time. This generates a better random approximation.

EDIT: Yaaaa Sane beat me to it and his explanation directs you to where you need to go. lol

Ghost Nov 19th, 2007 5:02 PM

Re: Complete Randomization
 
Hope this helps a little. Post your source when you're done if you don't mind. Could be a fun way to kill some time.
:

'Create a new Random class in VB.NET
Dim RandomClass As New Random()

Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(4, 14) ' 4 is the min and 14 is the max


Megabyte Nov 19th, 2007 5:21 PM

Re: Complete Randomization
 
Quote:

Originally Posted by Sane (Post 137140)
You have to give the random number generator an arbitrary starting point. This is most often done by passing the computer's epoch to "seed the random number generator". Since the epoch you give it will be different each time you start the program, the generator will then start with a different set of random numbers.

Read Random Numbers In Visual Basic. More specifically, sections "Initializing the Random Numbers" and "Repeating Random Numbers".

Hope that helps.

Thanks man.

:

Private Sub Form_Load()

Randomize

End Sub


Is that all I need then? It looks like it works but I just wanted to be sure.

Sane Nov 19th, 2007 5:31 PM

Re: Complete Randomization
 
Quote:

Originally Posted by Megabyte (Post 137146)
Thanks man.

:

Private Sub Form_Load()

Randomize

End Sub


Is that all I need then? It looks like it works but I just wanted to be sure.

Yep. :)

"Form_Load()" is a function that is called when the application's form is loaded at runtime. "Randomize" seeds the random number generator to a pseudo-random starting point. So the random number generator will provide a different set of numbers each time the form is loaded. That's all you should need.

This is assuming you're not worried about the quality randomness. For a school project, the numbers are fine. If this were a real slot machine, other programmers could figure out when the jackpot will occur by working backwards from the seeded value. Don't worry about it, it's only something to consider looking into to satisfy the curious mind.


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

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