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
Related
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)
The Random node will output the random value that is between 0 to 1. The initial i value should be zero. So for the first iteration, the comparator should output a signal TRUE to let the loop iterate again. When i is 2, the loop will stop, because the random variable is smaller than 2.
So as my thinking, the loop should iterate only two times and then stop, but the answer shown to says me the loop should iterate just 1 time. Is the answer wrong or is my thinking wrong?
I ran the same code and got 2 executions of the loop, as you'd expect.
Furthermore, since the random number generator cannot return exactly 1, the code is guaranteed to take exactly 2 loop cycles.
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
Spent about an hour now, with multiple rages at my monitor. I have a variable which stores the amount of elements within an array.
I want to add the numbers '1' up to this variable number to a combobox. So that when i use the combobox, it gives the options of 1,2,3 etc up to the variable number. If anyone could help, that would a fantastic!!
Also, I tried a few different loops but caused visual studio to give an unable to access debug error when i tried to run the program. I am new to this so apologies if this seems basic.
The code I have below stores the count.
accno = custdetails.Count
You don't need to write the loop. This is a one-liner:
MyCombobox.Items.AddRange(Enumerable.Range(1, accno).ToArray())
I prefer that because it starts you thinking in terms of matching your presentation to a data source, which will help you a lot as you learn further.
But if you really want to:
For i As Integer = 1 to accno
MyCombobox.Items.Add(i)
Next
Is it possible to display a single messagebox inside a for loop codes?
I am trying to but cannot successfully make it.
Please help.
Thanks in advance.
Code inside a loop, by definition, is meant to execute multiple times, for each iteration of it, so if you don't want to have multiple messages, move that call outside of the loop so it executes only once. This would be the main way of doing so, because it makes it clear that there will be ever only one of them.
Otherwise, you can condition its display, by some particular condition that's warranted to happen only once, possibly caching the result from the user. The second snippet from phadaphunk does something like that.
From you question, I get that you don't understand how it works below.
The loop is a set of instructions that must be executed "x" number of time. So everything that is in the body of that loop will be executed that number of time. Which means that if there are 5 iterations to the loop, you will get 5 messagebox.
This is how you do it if you want a different box for each iterations :
For Each x As Integer In someList
MessageBox.Show(x.ToString());
Otherwise you can't do it unless there is a certain condition that explains to the compiler at which iteration you want the message box to appear. Something like
For Each x As Integer In someList
If (x == 5)
MessageBox.Show(x.ToString());