Shortcuts

(Non Repeating) Random Numbers in VB.Net

I cant seem to get used to using arrays in VB.Net. On the code behind page, I need to select a random number between 1 and 25. That's easy enough: Dim myVal As Int16 Randomize() myVal = Int((Rnd() * 25) + 1) Next, I need to make sure the same random number
does not come up again. What's the best way to do this ? I'm guessing as follows: 1. Use an array that holds the values of all previously generated random numbers. 2. Check in the array to see if a number has been generated, if it has, either add or subtract
one from the current number. The problem is, I cant figure out the syntax. Id appreciate if someone could post a sample of (or link to) similar syntax. Also, if there's a better way to do this, please let me know. Thanks

Re: (Non Repeating) Random Numbers in VB.Net

Hi, Below is a class I put together to demostrate a possible approach to your problem. As an optimization, instead of storing the values in an array and scanning the array I store the value 1 at the index that way determining if a value has been used is a simple
matter of looking at the index that correspondes to that value if there is a 1 then it has already been used, if it is 0 the it is available. The code also checks a counter and throws an exception if there are no more possible unique values. You can add properties
that can return how many unique values are still available to avoid the exception etc. First to use this class:

Dim rnd As New RandomSequence(25)
For i = 0 To 100
System.Diagnostics.Debug.WriteLine(rnd.NextValue())
Next

Basically what this does is it goes through and pops one index from the array after another. But then to get the random number it only uses what is left in the array. So you will never have
to worry about checking what came before it.

Re: (Non Repeating) Random Numbers in VB.Net

Hi nberardi, What you need to becarefull of when removing items from an ArrayList is that it causes memory to be shifted around. For example if you remove the second item from the array the items following it are shifted down in memory. This shifting of memory
could have a negative impact on performance especially while the ArrayList is still large. Regards

Re: (Non Repeating) Random Numbers in VB.Net

Yeah but he was only using 25 instances, and that isn't going to effect anybody. He would probably need a better solution if he went to 1000 or 10000. But that also puts a damper on your idea too, because you would have to allocate all that memory in the begining
to hold all those objects. I also can't imagine there is all that much difference between the two because they are both going to have to use loops to go through the current array of values. I know that poping values from the stack uses a little bit of memory
and their may be a slight performace hit, but I tend to like the sleek and sexy code.

Re: (Non Repeating) Random Numbers in VB.Net

Hi, After considering your remarks on performance I thought to perform a benchmark out of interest. The following are the results of that test. Numbers...Iterations....Stack Solution....Array Index Sollution -----------------------------------------------------------------------
25............100000........05.7088350.......03.1849290 100..........100000........31.8292590.......07.3814235 200..........100000...01:41.0063175.......13.7612970 I did test with higher number of items, but eventually cancelled them because the stack based
sollution grew exponentially. I used Jon Skeet's
bench mark harness to perform the tests on a P4 1.4 W2K VS.NET 2003 release build. Regards

Re: (Non Repeating) Random Numbers in VB.Net

Hi, The stack solution is the solution you presented. I also got the chance to run the routines through a profiler, and as I expected it was the remove that was the biggest drawback to your solution. In your solution removing elements from the array list made
up for 54.1% of the time taken, while getting a random number took 1.4% of the time. For my solution, getting a random number was taking 56.7% of the time, the closer this can get to 100% the better as the focus of the routine is to get random numbers. Recognizing
your argument that both routines require excessive memory should the random sequence be very large, I could use a BitArray rather than the integer array which admittedly I was rather stupid to have used, it should at least have been a array of booleans. Hope
this helps