• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to randomly assign specific Values in an array or arrayList?  RSS feed

 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to build a monopoly like game, and atm I'm trying to find way how to build the Community and Chance chest cards. so far, my logic is
1-create an ArrayList of cards with a given order
2-for a given number of times(for loop) generate 2 random numbers ,which will be the parameters for Collection.swap().
3-swap.

here's the code for the shuffler Button


for now things seem to work pretty ok, but I'm wondering if this is a good and efficient way to shuffle a card chest especially in case of large number of cards. plus, I'm not sure what would be a good loop count for effective shuffling, in my case I used i<arraylist.size
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is already a static method in Collections class that does the job.
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which I did implement in my code. what I wanted to ask is how to shuffle my arraylist
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raed Tabani wrote:which I did implement in my code. what I wanted to ask is how to shuffle my arraylist


I believe Paweł was referring to the shuffle() method (of the collections class). And I believe that method does it a bit better than yours. Like yours, it loops exactly the size of the list, however, unlike yours, it is guaranteed to touch every member of the list.

Henry
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raed Tabani wrote:what I wanted to ask is how to shuffle my arraylist


Why do you want to reinvent the wheel?

The method I mentioned uses this algorithm:
javadoc wrote:This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive.


Henry Wong wrote:I believe Paweł was referring to the shuffle() method.

That is correct.
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not trying to do that. like Henry said, I thought you meant the swap() as I wasn't aware of the shuffle(), anyways thanks

 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Raed Tabani wrote:which I did implement in my code. what I wanted to ask is how to shuffle my arraylist


I believe Paweł was referring to the shuffle() method (of the collections class). And I believe that method does it a bit better than yours. Like yours, it loops exactly the size of the list, however, unlike yours, it is guaranteed to touch every member of the list.

Henry


so does the shuffle() generate just one random int?
is it something like this?
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it generates one random int. It does the swapping a little bit different than your loop.
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Yes, it generates one random int. It does the swapping a little bit different than your loop.

I was about to ask you why the shuffle() starts top to bottom and leaves the first element out, which I assume is to make sure that you don't swap the element with it self, case i= random, so that every shuffle counts?
not trying to reinvent the wheel but is this closer?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raed Tabani wrote:
I was about to ask you why the shuffle() starts top to bottom and leaves the first element out, which I assume is to make sure that you don't swap the element with it self, case i= random, so that every shuffle counts?
not trying to reinvent the wheel but is this closer?


Yeah, I think you got it. The "trick" of the algorithm, is that the randomize choice item to move must be from the group that hasn't been chosen yet (which of course, will get smaller and smaller). Without this, it is possible to move an item more than once, and hence, can no longer guarantee to move every item.

Henry
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A real simple way to do the same thing is keep keep cards in the arraylist in some meaningful order, then choose one at random using the random number generator. Delete it if you want ahd the new arraylist size is the new upper bound for your next random number. Same effect as shuffling, but smarter.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you correctly, in what way is it different from
what the shuffle method does, other than that you need a
second array list? (which, in itself, is not a bad thing).

But the shuffling method is not how I shuffle. I start
by zipping the two halves of the pile, et cetera. Now,
thát would be fun to implement, even if it may not
be as rondom as the shuffle method. For when OP
has finished the game.

Greetz,
Piet
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:A real simple way to do the same thing is keep keep cards in the arraylist in some meaningful order, then choose one at random using the random number generator. Delete it if you want ahd the new arraylist size is the new upper bound for your next random number. Same effect as shuffling, but smarter.

that is another way to look at it, Its definitly not a simulation of how you draw from chest card but it has same effect
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
But the shuffling method is not how I shuffle. I start
by zipping the two halves of the pile, et cetera. Now,
thát would be fun to implement, even if it may not
be as rondom as the shuffle method. For when OP
has finished the game.

that is a fun way to implement have you given it a try? I would love to give it a go tonight
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Piet,
this is what I got so far
I find it hard to describe how it works but it produces the same effect as the shuffling method you were talking about(if I understood you correctly)



the outcome for the given string is
6
1
7
2
8
3
9
4
10
5
11
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raed Tabani wrote:
that is a fun way to implement have you given it a try? I would love to give it a go tonight

hi Raed,

eh, no I haven't given it a try. When I was reading Guillermo's reply, I suddenly realized
that what he wrote was actually much closer to how I shuffle 'by hand' than this 'shuffle()'
method from the Collections.

But it was not my intention to break in into the discussions that are going on. How are
you doing with your monopoly game? My advice would be: concentrate on getting
that game up and running, using the 'shuffle' method or what Guillermo described.

But now you have made me curious, so I will look at your method! It is bedtime in Holland
so I might not reply for the next couple of hours, but 'I'll be back'!

Greetz,
Piet
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is interesting, the way I pictured Guillermo's method is more of a randomly "picking" a card from a fixed cardchest, instead of going for the card placed on top. but I agree, that shuffle() is no where near how we shuffle in real life, thank god ...oh by the way, is this the shuffle you were referring to?
riffle Shuffle

as for the game, I was just about to introduce a 2nd player when I realized a problem with my layout, so I guess in a way I'm back to the start but I will keep you guys updated.

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, is it called 'riffle shuffling'? I didn't know that, and that guy certainly
is much, much better at this than I am. I do sometimes have to pick up
a card or two from the floor.

Yes, but it is not quite what I do. I start by riffling as best as I can,
but sometimes more than one card from one of the two piles is taken.
Next, I start shuffling, by taking the pile in my right hand, dropping
somewhat at random one or more cards into my left hand, and so on,
until my right hand is empty. And sometimes I put these cards on top
in my left hand, or on the bottom, also somehow randomly. So, considering
my right hand and left hand as arraylists, I do use two arraylists.
And I repeat this procedure somewhat randomly a couple of times.

That's what I had in mind yesterday evening late. Now, there are two
interesting things: implementing my clumsy way of shuffling, and, since
I use it in practice, how 'random' is the result?

For instance, if I do this 52_000_000 times, I would expect the Queen
of Spades to appear about 1_000_000 times as the first card.

Anyone here having a non-rusty knowledge of the multinomial distribution?

Looking at what you did: it looks a perfect rffle shuffle, indeed!
But the outcome is not very random, so we have to do some shuffling...

Greetz,
Piet

Edit: if you do have some spare time with your monopoly and sudoku, have a look
at project euler (google for it), problem 84. A very nice puzzle indeed!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!