How to randomly shuffle pictures in visual basic - vba

I am trying to create a memory game, but I cannot seem to get the pictures to shuffle around everytime I press start. Does anyone know how to help?

Use Randomize once at the beginning of your procedure.
Randomize uses number to initialize the Rnd function's random-number generator, using the system timer as the new seed value. If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.
(Source)

Related

Visual Studio Fortran Stuck at Reassigning values from one variable to another

See attached screenshot.
I ran the code, stopped it at a break point, and F11 the steps until the line ccs2 = ccs2_2.
I checked both of these variables have the same dimensions, same number of elements that actually have value (they are both 6x6x10000 and only the first 6x6x219 elements have values).
I have ran this algorithm before and I have never had this problem. What you see in the screenshot is the visual studio stuck at this point with no error and no notifications for what seems like forever (been at least 10 minutes for each time I tried) when I pressed F11 to try to execute ccs2 = ccs2_2.
I checked I don't have any memory or CPU problems. When I pause the execution from the top, the progress marker goes back to the line and when I checked the variables, I find the first 6x6x219 values now match up, but the program won't advance forward.
Updates: I attempted to limit the range to just the elements that have values such as ccs2(1:6,1:6,1:219) = ccs2_2(1:6,1:6,1:219). It still takes a long time, but for some reason, visual studio reports that it only took 2059 ms, but the actual time was definitely way more than 20 seconds. I changed heap arrays from 0 back to nothing and the new time is supposedly 1978 ms but it definitely took more than 19 seconds.
I tried using an explicit loop to assign only the elements with value and it took less than 1 ms.
When you step over a huge array assignment statement, the debugger, internally, does single step by instruction until the statement number changes. I see your arrays are very large, and the debugger isn't "stuck", it is just taking a long time to step over.
Try as an alternative setting a breakpoint on the next statement and hitting Go.

What is the best/fastest method of filling an array with numbers between two values?

I've got a background thread which adds numbers to an array, this call happens often so i'm just curious if there's a faster method of adding the numbers to the array?
I need to add all the numbers between two values (values will be different depending on the situation), example 1 to 4 which would add 1,2,3 & 4.
However, when I make the call, it would be adding much larger arrays similar to 500 to 1000 etc which sometimes takes a little longer depending on how many numbers need to be added.
At the moment i'm using something similiar to this:
Dim ListOfNumbers As New List(Of Integer)
For i = 1 To 100000
If ListOfNumbers.Contains(i) = False Then
ListOfNumbers.Add(i)
End If
Next
Is there any other method that I could use which might be faster?
(Avoiding duplicate values in the array if possible)
I doubt this is driving your program's performance. Moreover, the best option may depend on how you plan to use these numbers, where you may find you actually can noticeably improve performance by using something like IEnumerable throughout your code, rather than an array.
With that in mind, I suggest this option:
int start = 1; int stop = 100000;
var list = Enumerable.Range(start, stop - start).ToArray()
Such that you can easily remove the ToArray() later if desired.
As for the existing code... You control both the List and the loop. Checking .Contains()is not necessary, and probably taking up a significant part of the execution time here. Just remove that check. You can also optimize some by pre-setting the size of the list:
Dim size As Integer = 100000
Dim ListOfNumbers As New List(Of Integer)(size)
For i = 1 To size
ListOfNumbers.Add(i)
Next

Find Min/Max from 1 textbox

I'm trying to find the Min/Max value of a textbox that has 1 variable and display it to the user, so the variable changes everytime the button is clicked. How would I find the maximum value of something that is constantly changing? The trick is that I can NOT use if statements or case statements. I'm totally at a loss here.
Ok, the things that limit you.
One variable
no if/case statements
The lesson seems to revolve around using Math.Max().
Math.Max(), as we can see on MSDN returns
the larger of two 32-bit signed integers.
The one variable we are going to use needs to exist outside of the button's click event. So, just make it a class variable.
This variable will essentially store the largest value. Math.Max() returns the largest of two values... see what I am getting at here? You can pass the current largest variable as a parameter to Math.Max() without any issues.
Example:
Dim max As Integer
max = Math.Max(1, 100)
'max would be 100
max = Math.Max(max, 10)
'max would be 100
max = Math.Max(max, 1000)
'max would be 1000

Visual Basic Array Push

I am currently iterating through a loop and redimensionalising my array every time I need to add a value (as you can imagine this takes some time for a redim every loop) is there a way I can implement a push similar to ruby or java? This would need to save the processing time needed to redimensionalise the array every time I need to add a value to it.
Cheers
Martin
You'd be better off using a List (Of Type). Then you can just call the Add method.
For example:
Dim foo As New List(Of String)
foo.Add("Bar")
You can concat the array, with a array containing only the new item, or multiple.
Array.Concat({Item}).ToArray

vba more realistic random number generator

I know I can use a quasi-random number generation function/variable called Rnd. However, I've noticed that whenever I used Rnd in my userform, this sequence of numbers always show up:
first iteration: 0.705547511577606
second iteration: 0.533424019813538
...
As a result, b/c the sequence of numbers showing up are the same every time when I relaunch the userform, it doesn't feel random. Are there other functions in the VBA function set that would make it feel more random? Thanks in advance.
Try adding a single call to Randomize Timer before you do any calls to Rnd. This will seed the random number generator using the time of day.
I don't know a lot about VB but I think you need to seed your number generator. I think Randomize does it for VB.
You could manually set the seed based on the time of day... insert something like this at the top of your vba code:
For i = 0 To (CInt(Format(Time, "ms"))) 'minutes and seconds turned to integer
test_random = Rnd()
Next