![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hand Crafted
Join Date: Nov 2007
Location: The Pit
Posts: 13
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() |
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. |
|
|
|
|
|
#3 |
|
King of Portal
|
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
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#4 |
|
Man Bear Pig Hunter
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#5 | |
|
Hand Crafted
Join Date: Nov 2007
Location: The Pit
Posts: 13
Rep Power: 0
![]() |
Re: Complete Randomization
Quote:
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. |
|
|
|
|
|
|
#6 | |
|
Programming Guru
![]() ![]() |
Re: Complete Randomization
Quote:
"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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pong game complete | teencoder | Show Off Your Open Source Projects | 6 | Aug 9th, 2006 3:27 PM |
| Assembly for complete beginner? | Flavius_Belisarius | Assembly | 17 | May 14th, 2006 3:06 AM |
| complete C++ OS??? | Kilo | C++ | 10 | Dec 6th, 2005 11:03 PM |
| complete newbie question | melee28 | C | 36 | Sep 11th, 2005 7:48 AM |