Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

how to random shuffle an array?

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-30-2007, 04:24 PM
Basic4ppc Veteran
 
Join Date: Apr 2007
Posts: 269
Default how to random shuffle an array?

Hi,

I need to shuffle an array with numbers in a random way, let's say cardnumbers (in fact something else that has to do with neural networks but the same procedure).
Now I think this has to be a loop, run at least once in which the condition has to be that the array already holds the number in which case a new randomnumber is set, looping unitl this number is not yet in the array.
Like:

FOR c = Nr1 to Nr2+1
DO
x = RND(Nr1,NR2)
UNTIL (x NOT in ArrayOfNumbers())
ArrayOfNumbers(c) = x
NEXT

Or does anyone have a better solution?
tnx
Marc
Reply With Quote
  #2 (permalink)  
Old 06-30-2007, 05:28 PM
LineCutter's Avatar
Senior Member
 
Join Date: May 2007
Location: Daarsit
Posts: 161
Awards Showcase
Beta Tester 
Total Awards: 1
Default

I've not got this into code yet, but I have a similar need to select from an array (in my case I want a subset of the original array)

Create 2 arrayslists: one with the ordered data, the other for the shuffled data.
Pick the nth element of the ordered arraylist (where n=rnd(size of ordered array))
Append this value to the shuffled arraylist
Remove this entry from the ordered arraylist
Repeat until the ordered array is empty.

Note that the size of the ordered arraylist shrinks, so the random number is always a valid element (& is never a duplicate). No futile loops.
Reply With Quote
  #3 (permalink)  
Old 06-30-2007, 05:47 PM
Basic4ppc Veteran
 
Join Date: Apr 2007
Posts: 269
Default

Hey LineCutter, that is a neat solution!
Tnx
Reply With Quote
  #4 (permalink)  
Old 06-30-2007, 05:59 PM
Basic4ppc Veteran
 
Join Date: Apr 2007
Posts: 269
Default

This DOES call for a shuffle property in arraylist.... Erel? Also usable when developing cardgames!
Marc
Reply With Quote
  #5 (permalink)  
Old 06-30-2007, 07:01 PM
Cableguy's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: N 41º11'30.30" W 8º39'46.60"
Posts: 2,344
Awards Showcase
Forum Contributer 
Total Awards: 1
Default

I don't think this is good for an arraylist, as after randomizing we no longer know were a particular data is....I think the best solution would be to create a standart control, maybe called RandomList??

This calls for action on our beloved dll creators...
__________________
Paulo Gomes - Porto, Portugal - Living/Working in France
Mobile Device: Samsung Galaxy S, Android 2.3.4 CUstom ROM
Laptop: Toshiba NB100-130 (running on Win7Ultimate)

My Posts helped you? Consider Buying me a Porto Glass!
Reply With Quote
  #6 (permalink)  
Old 06-30-2007, 08:49 PM
RandomCoder's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Location: Derbyshire, UK
Posts: 623
Awards Showcase
Beta Tester 
Total Awards: 1
Default

CableGuy,

You had a similar problem to this with your pairs game and as I recall I made you I snippet of code (sorry but I've lost it now ) whereby I placed all the image names into a String separated with commas.
All that had to be done was randomly select any point in the String and find the first comma then remove the image name and add it into the desired position in your grid. The string is made shorter because the entry was removed and so as LineCutter put it, there's NO futile loops!

Sounds like you need a similar thing with your Array of numbers. If you want the code snippet I think it probably best to cut and paste from your pairs game.

Regards,
RandomCoder.
__________________
"Defeat never comes to any man until he admits it."Josephus Daniels
Reply With Quote
  #7 (permalink)  
Old 06-30-2007, 09:03 PM
Cableguy's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: N 41º11'30.30" W 8º39'46.60"
Posts: 2,344
Awards Showcase
Forum Contributer 
Total Awards: 1
Default

Thanks RandomCoder, I still have the original code stored here....I do not need it, was StellaFerox who need it...

Still a control would come in handy!
__________________
Paulo Gomes - Porto, Portugal - Living/Working in France
Mobile Device: Samsung Galaxy S, Android 2.3.4 CUstom ROM
Laptop: Toshiba NB100-130 (running on Win7Ultimate)

My Posts helped you? Consider Buying me a Porto Glass!

Last edited by Cableguy : 06-30-2007 at 09:08 PM.
Reply With Quote
  #8 (permalink)  
Old 06-30-2007, 09:47 PM
Basic4ppc Veteran
 
Join Date: Apr 2007
Posts: 269
Default

would be handy though, because shrinking the array still holds the possibility that several times one of the first positions is chosen randomly.
Reply With Quote
  #9 (permalink)  
Old 07-01-2007, 08:49 AM
RandomCoder's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Location: Derbyshire, UK
Posts: 623
Awards Showcase
Beta Tester 
Total Awards: 1
Default

I think we may have crossed wires Stellaferox,
My solution involves randomly selecting your numbers from a Sting that is made up of all the numbers you require in the array. The numbers are placed into the array in the next available space so presuming that you clear the Array before you start, by the time you have removed each number from the String the Array will be randomly filled.

Regards,
RandomCoder.
__________________
"Defeat never comes to any man until he admits it."Josephus Daniels
Reply With Quote
  #10 (permalink)  
Old 07-01-2007, 10:48 AM
Basic4ppc Veteran
 
Join Date: Apr 2007
Posts: 269
Default

I placed my solution in a new post. Somehow the code doesnt work.....
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 Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
The same random numbers LineCutter Questions (Windows Mobile) 4 10-16-2008 05:05 PM
Nullifying an Array mjcoon Questions (Windows Mobile) 1 06-20-2008 05:20 PM
Array but not array Cableguy Questions (Windows Mobile) 1 09-22-2007 08:09 PM
Array of Array possibility? Stellaferox Basic4ppc Wishlist 0 09-18-2007 12:16 PM
More Dimensional Array Xardas Questions (Windows Mobile) 4 07-01-2007 02:11 PM


All times are GMT. The time now is 08:26 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0