I've made a timer and six labels. Then a number is being generated for each label seperately.
After testing how it works, I found out that sometimes two same numbers are being generated at the same time.

In the picture attachment you can see how two numbers were generated at the same time. I was wondering if it's possible to prevent the random generation of two numbers at the same time for the numbers in the blue box.

Here is the code that is in the timer:

``````        Randomize()
Dim Number1, Number2, Number3, Number4, Number5, Number6 As Integer

Number1 = Int(Rnd() * 76)
LabelM1.Text = Number1

Number2 = Int(Rnd() * 76)
LabelM2.Text = Number2

Number3 = Int(Rnd() * 76)
LabelM3.Text = Number3

Number4 = Int(Rnd() * 76)
LabelM4.Text = Number4

Number5 = Int(Rnd() * 76)
LabelM5.Text = Number5

Number6 = Int(Rnd() * 16)
LabelM6.Text = Number6
`````` ## All 21 Replies

You want to know how to generate a list of unique random numbers.

One method would be to keep track of all the numbers you have perviously generated, and if the rng comes up with a non-unique number you reroll.

This works well if the range is big and you any need a couple of numbers.

An optimization might be to use a sparse hash implementation.

Another method would be to make a list of numbers in the entire range and "shuffle" the list (Fisher–Yates shuffle).

This works well if the range is the same size of the numbers required.

Another thing I forgot to consider is that I need the first five numbers to match regardless of the position, as long as the five numbers on the bottom match those on the top it's good. The last one is an exception and needs to be matched to be the same in that possision.

So what is the best way to make this work?

Generate the 5 unique random numbers on top, and then shuffle them to make the numbers on the bottom with something like Fisher–Yates shuffle.

OK. However I never worked with this. Could you give a specific example of how to do the suffling and apply the Fisher-Yates method?

From wikipedia:

``````To shuffle an array a of n elements (indices 0..n-1):
for i from n − 1 downto 1 do
j ← random integer with 0 ≤ j ≤ i
exchange a[j] and a[i]
``````

Make sure you get this right, otherwise you might end up with soem bias.

The numbers on top is the users input. I want the program to randomly generate numbers to match those which the user entered.

Fine. Get the numbers from the user and then shuffle them into another order.

How would that be done by code, I never did that before?

I would suggest that instead of shuffling you start with a list of all possible numbers. Then for each new selection you just generate a random index from 0 to list.Count - 1. Once you retrieve the number at that index, remove it from the list.

I don't need the numbers to be matched one by one, instead I want all numbers to be randomly generated all at once until all numbers match at once.

From my understanding, you want the user to enter in 5 numbers, and the program gives back a random perumation of the numbers. ie, if the user gives `5, 7, 1, 2, 9` an example of an output would be `7, 5, 9, 1, 2`.

How would that be done by code, I never did that before?

Make an array. Store the users input into the array. Suffle using the described shuffling algorithm. Display the shuffled array.

If your asking us to do it for you, we wont. If we did, you wouldn't learn.

Jim's suggestion is a variation of the Fisher–Yates shuffle (instead of swapping, he's storing it in a seperate list or displaying it directly). The final effect is the same (unless you're trying to generate a random subset).

unless you're trying to generate a random subset

It is exactly what I am doing.
The user can enter any number 1 - 75 and not using the same number more than once. Then the code above generates 5 sets of random numbers also 1 - 75. The timer will stop generating numbers as soon as the five numbers were generated randomly.

So if the user will enter: 3 21 17 29 66
as soon as the program gets: 66 17 21 3 29
the program will stop

So like a slot machine?

Store the 5 numbers the user gave you in a list. Generate a number. If the number exists in the list, remove it (or mark it somehow) and display it. Else, keep rolling numbers.

If the number exists in the list, remove it

Why would I do that for?

And how would I

Store the 5 numbers the user gave you in a list

???

If the number exists in the list, remove it

Why would I do that for?

So you know which numbers you have left to match. That way if you get a number, and it's not in the list, then the number is either irrelevant or already found.

And how would I

Store the 5 numbers the user gave you in a list

Do you need some tutorials on VB programming? If you don't know how to use an array, then thats where I would suggest you start.

So you know which numbers you have left to match. That way if you get a number, and it's not in the list, then the number is either irrelevant or already found.

To make myself clear, all five numbers are being generated at the same time over and over non stop even if one of the numbers is matched.
The user is only allowed to enter 1-75, and the computer generates the numbers 1-75 as well, so I don't see what you mean by:

the number is either irrelevant

First off if you don't want to allow 0 to be generated you need to use this:
Dim value As Integer = CInt(Int((76 * Rnd()) + 1)).
Here is what I would do: Grab the numbers from the user,leave these in the textboxes but also copy them into an array and sort them there. Generate five random numbers out of the 75, put these into the other boxes and then copy them into another array - sort them there. If both arrays match leave them as they are in the textboxes, if they dont match clear the random array clear the textboxes and generate the next five numbers etc. Once the sorted boxes matcxh you are done, stop the timer and see how long it took.

Dim value As Integer = CInt(Int((76 * Rnd()) + 1)).

That's exactly what I've changed the code to, however, I was not able to prevent two same numbers to be generated at the same time.

I run the program and sometimes get two same #'s:

71 07 10 07 48 10

In the case above, two 10's and two 7's were generated at the same time.

Is there a way to solve this nuiance OR make a pre-code which will delete the generated number if it contains repeated number before comparing to the user's input? So if the generated code does not contain a repeated number, then compare it to the user's input. At least this is one of the ways I see to solve this? Effective? Don't know if there are any better ways than that.

If you generate a random number, check if it is already in your list. If it is, generate anotherone.

You generate the five numbers in the following way. First number generated you put in the first textbox and also you copy it in an array. 2nd number you put in the array and sort it. There are thre possibilities: <,> and =.
So you start off with: if it is equal, forget it and get the next number. If it is smaller,move it before the first number and and put it in the second textbox and so on. Once you have the five numbers compare the this sorted array to the one you have generated, sorted from the user input. If they match you are done other wise start over again in your loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.