Random Numbers

No Repeating Numbers

Page 1 of 1

3 Replies - 4410 Views - Last Post: 27 May 2008 - 08:15 AM Rate Topic: -----

#1 WesQ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: Random Numbers

Posted 27 May 2008 - 07:24 AM

Hi!

If I understand your question correctly and you are using VB6 then look at this page:
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
Was This Post Helpful? 0
  • +
  • -

#4 clargs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1