Geeks With Blogs
Code.Blog Code Talk for the Game Developer

Recently I made a very simple Random Number Generator (only took a few hours).  The purpose was to build a program for a football pool and the user needed a program that would have two types of functionality:

  1. Need 10 random numbers (0-9) and allow repeating digits, but digits cannot be next to each other.  The output should be put into a comma delimited file.  This needs to be repeated 38 times.
  2. Need 10 random numbers (0-9) but do not allow repeating digits.  Output to a comma delimited file.  This should be repeated 2 times.

Of course, I could make a basic, statically-coded, console app but I chose instead (for fun) to build it into a Win32 app in VB.  Here's a quick screen of the program:

I accept a low number and high number (in these cases, 0 for low and 9 for high), as well as how many rows and columns.  Columns are the count of numbers you need (in this case, 10 numbers) and rows are how many times the program should repeat (in case 1, 38; in case 2, 2).  Next, a check box can be used to prevent repeating numbers, as needed in example 2.  Then, you can browse to a file or use the Save File dialogue that opens when Browse.. is clicked, to determine the file being outputted to.  Since the text box is ReadOnly, a clear button clears the box.  Finally, there's a Generate button (which obviously runs the generator) and an output window that displays the numbers that are generated (which is space-delimited for readability).

EDIT: I've removed the base code because the blog erased almost all of it.  I'll post the source on my site later and link it.

The first GenerateRandomNumber (the one which accpets no arguments) is used for the second version of this program, the one requiring no repeating digits.  The second version of this method (the one accepting arguments) simply returns a random number.  Now what is very important is that while the method receiving arguments returns the random number to be used, the one accepting arguments DOES NOT!  It actually returns a random number to be selected in a list.

Private Function GenerateRandomNumber(ByVal iLow As Integer, ByVal iHigh As Integer) As Integer
'Randomize number
Return CInt((iHigh - iLow) * Rnd()) + iLow
End Function

The Method

For the first version of this program, all I need to do is grab a random number, and if that number is the same as the previous, re-generate a new number and check it again.  This is done above in a while statement.

For the second method, I initially created a list to store the numbers in, then each time I would generate a number, check to see if it was in the list, and if it was I would re-generate a number (similar to the first way and looking at the previous number except now I have a list instead of one number).  Now an extreme problem came up, here's the basic example of that problem.  Let's assume we're on the first row and can select numbers 0-9 and require 10 numbers.  The random generator runs 8 times and in some order produces the numbers 0-8, leaving only the 9 left.  Now, the generator will loop over and over and over until it randomly guesses 9, the only remaining available number.  Maybe that doesn't seem hard, but it gives the computer a 1/10 chances to guess it.  Now repeat that 2 times, how about 38 times?

So, what I implemented was a simple list that stored the numbers from the low to high.  If you start the program and type in low as 0 and 9 as high, the program builds 2 lists, one that is completely empty, and one that is filled with each number between those values (0, 1, 2, 3, ..., 9).  Now the generate method runs and picks a position (let's say 3) and places that positions value into the blank array (now contains: 2).  Next the generate grabs number 0 (now contains: 2, 0).  Uh-oh, it again chooses 3!  But, since we removed that number from the list, position 3 is now containing the next number in the sequence, 3.  Blank array now contains 2, 0, 3.  And so on.  Then, there's an added part to the generate stating, if the array containing the numbers to choose from only has a size of 1, we only have 1 value left and it will be stored in position 0 so we can automatically assume to return position 0.

Problems

This method has a few problems though.  The most immediately concerning, is if the user decides to generate random numbers without repeating digits (method 2) but decides to create more columns than numbers.  For example, we have numbers 0-9, but the user requires 11 columns.  Our generate will run but will constantly be searching for another number and the array will run out and all sorts of messes occur.  I've included protection against this in the final code (via CheckChanged on the check box and a Leave check on the columns box).

Secondly, and I have no idea why this happens, if you run the first method and then run the second method right after, the first row has repeating digits in it.  But, if you run the second method more than once, it works fine.  I'll look into this error if I have time to revisit this program.

I'll post the code on my website later if you want to grab the source.

Posted on Monday, August 18, 2008 10:50 AM Tutorials | Back to top


Comments on this post: Random Number Generator (VB Edition)

# re: Random Number Generator (VB Edition)
Requesting Gravatar...
Please send me code of Random Number Generator.




Thanx
Left by Roshan Kumar on Sep 17, 2008 2:21 AM

# can you please send be the code
Requesting Gravatar...
thank you!
Left by Deagle on Oct 08, 2008 6:58 AM

# re: Random Number Generator (VB Edition)
Requesting Gravatar...
Can you send me your code?
Thanks!
Left by Marcel Verberg on Feb 18, 2011 12:35 AM

# re: Random Number Generator (VB Edition)
Requesting Gravatar...
please send me your code
thank you

my e-mail: s.bafkar@gmail.com
Left by sevda on Nov 04, 2011 3:33 PM

# re: Random Number Generator (VB Edition)
Requesting Gravatar...

can you please send me the code, I 'm having problems creating it for a project. Thanks!!

thatone1282@yahoo.com
Left by KW on Dec 01, 2011 7:42 PM

Your comment:
 (will show your gravatar)


Copyright © Matthew Christian | Powered by: GeeksWithBlogs.net