Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Questions & Help Needed
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Questions & Help Needed Post any question regarding Basic4ppc.


FASTEST way to make a RND number list, no doubles


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-30-2008, 02:03 PM
Basic4ppc Veteran
 
Join Date: Apr 2007
Posts: 200
Default FASTEST way to make a RND number list, no doubles

Hi guys,

What would you think is the fastest way to generate a list of random numbers, without any doubles.
At the moment I am using the following loop:

do
generate random number
do
check whether number is in arraylist "uniques" (building if not)
if so EXIT and repeat generate random numberloop
until noDoubles
UNTIL all numbers are generated

I want to use this for shuffling a deck of cards, but then a million times to simulate the probability of certain "hands" in poker.

thnx in advance

Marc

PS: I had a thread a while ago concerning this problem but never tried to improve the speed.
Reply With Quote
  #2 (permalink)  
Old 01-30-2008, 02:32 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,770
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

I don't think you can get much faster than this. It doesn't generate unwanted random numbers. Every generated number is valid.

Code:
Sub Globals
	'Declare the global variables here.
	Dim Deck(52) ' holds the shuffled cards
End Sub

Sub App_Start
	AlCards.Clear ' ArrayList - clear in case used before	
	For i = 0 To 51
		AlCards.Add(i)	' stick all the cards in the array list
	Next
	For i = 0 To 51
		r = Rnd(0, 52-i)
		Deck(i) = AlCards.Item(r) ' add card to deck
		AlCards.RemoveAt(r)	'remove it from array list
	Next
	For i = 0 To 51 ' this bit just for display
		msg = msg & " " & Deck(i)
	Next
	Msgbox(msg)
End Sub
Reply With Quote
  #3 (permalink)  
Old 01-30-2008, 05:47 PM
taximania's Avatar
Basic4ppc Veteran
 
Join Date: May 2007
Location: Derbyshire. UK
Posts: 238
Awards Showcase
Beta Tester 
Total Awards: 1
Default

In an earlier project of mine, (ahem)

I picked 2 random numbers and swapped the array contents.

eg.

Dim Deck(52)
temp=0
b=0
c=0

For a = 1 to ?? 'as many times as you want.
b=rnd
c=rnd
temp = deck(b)
deck(b)=deck(c)
deck(c)=temp
next a

This would work for shuffling the cards.
Not sure it answers your question though
__________________
Endemol
Waste of space

O2 XDA Artemis Touch Flo 4.02 Full Cube Rom WM6.1

http://www.taximania.net
Reply With Quote
  #4 (permalink)  
Old 01-30-2008, 11:17 PM
LineCutter's Avatar
Senior Member
 
Join Date: May 2007
Location: Daarsit
Posts: 118
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Better late posting to this thread than never, even if I was here first
Shuffling cards (proof of concept)
Reply With Quote
  #5 (permalink)  
Old 01-31-2008, 07:01 AM
Basic4ppc Veteran
 
Join Date: Apr 2007
Posts: 200
Default

Hi Linecutter,

Thnx for your answer. I remember the thread (think I started it) and I used your solution in a different program. Now I wanted to know what the fastest way (after compiling) would be, as I like to use this in a simulation.
Marc
Reply With Quote
  #6 (permalink)  
Old 01-31-2008, 09:20 PM
LineCutter's Avatar
Senior Member
 
Join Date: May 2007
Location: Daarsit
Posts: 118
Awards Showcase
Beta Tester 
Total Awards: 1
Default

I don't think that you can do better than agraham's (or mine) for the "player vs. computer" scenario. Then again if you are going to do a million simulations you could probably take some shortcuts...
  • Just draw a "hand" of cards from the unshuffled pack, rather than shuffling & then drawing a hand
  • Only compare suits if they are going to matter (most hands will be determined by face value & not sets of the same suit)
Reply With Quote
  #7 (permalink)  
Old 02-01-2008, 07:40 PM
Basic4ppc Veteran
 
Join Date: Apr 2007
Posts: 200
Default

Thanks, that is surely one approach that will work. I thought of creating a limitless gigantic "deck" and drawing hands from that and then discard the hands that hold thesame cards. Maybe that would speed up the process.....
Marc
Reply With Quote
  #8 (permalink)  
Old 02-01-2008, 11:08 PM
LineCutter's Avatar
Senior Member
 
Join Date: May 2007
Location: Daarsit
Posts: 118
Awards Showcase
Beta Tester 
Total Awards: 1
Default

I don't think that that's going to help...

[A couple of assumptions: you are drawing 2 hands to see which wins vs the other & that there are 52 unique cards in the raw materials (deck) for each hand of the competition)]

The chance of a duplicate card, in a 2*5 card game is: 1/51 + 1/50 + ... 1/42. Very roughly that's 10%. i.e. you'll have to redraw 1/10 of your hands, plus 1/10 of those.

That would mean that 1/9 of all attempts to play would be invalid, or alternatively 1.111111 times more attempts are required in order to complete your run than if you'd adopted a more foolproof method of drawing a hand.


You will probably gain more from streamlining your question than speeding up the iterations...

Reference points:
My own software (in <ahem> another basic & archery related)

MonteCarlo Method
Bootstrapping
Reply With Quote
  #9 (permalink)  
Old 02-06-2008, 01:16 PM
Basic4ppc Veteran
 
Join Date: Apr 2007
Posts: 200
Default

@ Linecutter,

yes you're right of course. Your assumptions are correct. I thought that being "off" a percent or two wouldn't matter for a guess in winning probabilities. But then, as ever, I want to make it perfect first, then maybe "cheat" later....gaining speed.
BTW: very interesting software you have there. I am a great fan of simulations, statistics etc.
At the moment I am programming dealing hands and an evaluation routine for the dealt hand. Then I will make some tables for faster access and computing probabilities by doing simulations. i have to give in some to compensate for the lack of speed on a ppc here I think.
Thnx
Marc
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the PDA type and make HARRY Questions & Help Needed 3 01-13-2008 12:19 PM
How do I make a textbox go from one to the other sunnyboyj Questions & Help Needed 2 12-17-2007 09:44 PM
fastest way to search in database Put Claude Questions & Help Needed 14 10-10-2007 09:53 PM
Fastest way to get pixel colour value? DavidN Questions & Help Needed 6 09-02-2007 07:40 AM
Help me to make HTMLViewer Lib conf Questions & Help Needed 2 06-17-2007 05:35 PM


All times are GMT. The time now is 03:56 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0