# Random Numbers

Page 1 of 1

## 3 Replies - 5155 Views - Last Post: 27 May 2008 - 08:15 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=53071&amp;s=d77842da23b420fbe34f255e3f445256&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 WesQ

Reputation: 0
• Posts: 1
• Joined: 26-May 08

# Random Numbers

Posted 26 May 2008 - 02:16 PM

```	 Sub deal()
Dim i As Integer
For i = 0 To 11
face(i) = randomdeal.Next(0, 11)
Next i
End Sub
```

I know how to generate and use random numbers, but making them not repeat is a challenge to me. I am making a card matching program where the player has to flip cards and match them. The only thing is, when I deal the cards, I get 3 or only 1 of a specific card sometimes. Any suggestions?
Is This A Good Question/Topic? 0

## Replies To: Random Numbers

### #2 Zhalix

• D.I.C Regular

Reputation: 20
• Posts: 260
• Joined: 07-May 08

## Re: Random Numbers

Posted 26 May 2008 - 07:07 PM

Well, if it's only 12 possible cards, it's probably feasible to just check if the card has been used too many times and loop through over and over until it lands on only a good card.

I've actually made a program that had to deal with numbers between a small range, but it was large enough that looping through over and over would start to take a large amount of time near the end (since I was trying to find a unique number between that range when nearly all were used up). It was a number from 1 to about 200.

I started with a full list of all the possible numbers, 1 to 200.

What I did was every time the loop ran, it created an array with all the possible numbers to land on (in your case cards).

And every time I used a number (for you it'd have to be every two times the number was used), I crossed the "bad" number out of the good list (removed it)

So I had the good list constantly shrinking. Every time it ran it would split the good list into an array.

From that point, I just used the simple system of random numbers, except I used it to determine which index to land on (I had to specify the Ubound of the array because it changed every time). Then after I had selected my number to use, I crossed the new number out of the good list and the whole process started again.

There's probably a better way to do it, that's just what I came up with when I came across this problem.

This post has been edited by Zhalix: 26 May 2008 - 07:20 PM

### #3 oldSwede

• D.I.C Regular

Reputation: 4
• Posts: 464
• Joined: 08-January 16

## Re: Random Numbers

Posted 27 May 2008 - 07:24 AM

Hi!

http://www.vbexplore...m_numbers_1.asp

In short:
There's a function "randomize" that you should call before you use the function rnd.

/Jens

### #4 clargs

Reputation: 0
• Posts: 20
• Joined: 17-May 08

## Re: Random Numbers

Posted 27 May 2008 - 08:15 AM

This is a bad programming practice but it will solve your problem. I've modified your code so you can get the idea.
```
Sub deal()
Dim i As Integer
For i = 0 To 11
tryAgain: 'this is a label
face(i) = randomdeal.Next(0, 11)

for j=0 to i-1
if face(i)=face(j) then 'it will check if the current value appeared in the previous elements in the array
goto tryAgain  'if it did appear then it will go and generate a new random number
end if 'until it doesn't appear
next

Next i
End Sub

```

I've writen it with "goto". You can try writing it with while for a good practice.

Let me explain the idea. Assume we have an array from 0 to 5 which can contain 6 elements.
And we are generating random numbers and pushing them into the array.

Assume rnd() function 35.
Since theres no 35 in our array we can now push the value in.
Now we have 35 ? ? ? ? ?

Then 46 for example
theres also no 46 so we can push 46 too
Now we have 35 46 ? ? ? ?

Then let it again generate 46
Now after the control we see that theres 46 in the array
Then we tell the machine to go back and generate another
If the new generated number is not in the array (let it be 22), we can push it in
Then we will have 35 46 22 ? ? ?

And it goes like this. Of course this method is fast for small arrays (like yours).

Allways use Randomize before using Rnd() function

Heres an example

```Randomize
myRandomNumber = Rnd() * 100
```

Happy Coding
clargs

This post has been edited by clargs: 27 May 2008 - 08:17 AM