Re: Random numbers with no duplicates
From: Gary Labowitz (glabowitz_at_comcast.net)
Date: 09/27/04
- Previous message: Alwyn: "Re: ios::failbit always true"
- In reply to: David White: "Re: Random numbers with no duplicates"
- Next in thread: Spacen Jasset: "Re: Random numbers with no duplicates"
- Reply: Spacen Jasset: "Re: Random numbers with no duplicates"
- Reply: Francis Glassborow: "Re: Random numbers with no duplicates"
- Reply: David White: "Re: Random numbers with no duplicates"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 27 Sep 2004 08:41:36 -0400
"David White" <no@email.provided> wrote in message
news:ufO5d.2860$pl.51123@nasal.pacific.net.au...
> "Gary Labowitz" <glabowitz@comcast.net> wrote in message
> news:oJydnQObS4S3CsrcRVn-tA@comcast.com...
> > "David White" <no@email.provided> wrote in message
> > news:n7I5d.2844$pl.50584@nasal.pacific.net.au...
> > > That's not right. The last card would always be swapped with itself,
> > leaving
> > > the array as it is.
> > >
> > > BTW, another feature of this algorithm is that you only need to fill
the
> > > array once for any number of repeat uses. Once all values have been
> > > selected, they are all still in the array. They are in some
> pseudo-random
> > > order, but that doesn't matter because the starting order is
irrelevant
> to
> > > the randomness of the selections. Just reset the range to 10 and
repeat
> > the
> > > process.
> > If on the last step the tenth card is swapped with a randomly selected
> > location, there is a pretty good chance that it will not be swapped with
> > itself.
>
> On the last step there's only one location left that can be selected.
>
> > The algorithm is to swap each location in the array (from 0 - (n-1))
with
> > randomly selected locations in the range 0 - (n-1).
>
> No, the algorithm is to randomly generate an index in the range 0..N-1
> inclusive, where N is the number of values remaining, and swap the value
at
> the selected location with the value at N-1. Then you decrement N, which
> places the selected value outside the selectable range for the next
> selection. Here are the relevant parts from a card dealer class:
>
> CardDeck::CardDeck()
> {
> srand(/* suitable seed */);
> cards_remaining = N; // e.g., 52
> for(int icard = 0; icard < N; ++icard)
> deck[icard] = icard;
> }
>
> int CardDeck::DealCard()
> {
> if(cards_remaining == 0)
> return -1; // error, deck empty
> int icard = rand() % cards_remaining; // crude, and not ideal, but
> good enough for most purposes
> int card = deck[icard];
> deck[icard] = deck[cards_remaining-1];
> deck[cards_remaining-1] = card;
> --cards_remaining;
> return card;
> }
>
> void CardDeck::Shuffle() // i.e., to re-deal
> {
> cards_remaining = N;
> }
We're dancing around two different flag poles. I'm under the assumption that
shuffle works as follows:
Select a location in the array. This selection process will proceed from [0]
to [n-1].
Generate a (pseudo-)random number between 0 and n-1.
Swap the selected location with the randomly selected location.
Repeat the above process until you have done n swaps.
What you indicate doing is only swapping elements that remain after each
swap. I. e., you are keeping track of how many swaps have already been done
and only doing the remaining locations. This biases the swapping a bit. For
example, in a swap of 52 locations (cards) if the random number 50 doesn't
appear in the first 49 generations (due to a duplicate of, say 33, and
assuming the rest all appear once) then the next to the last card is always
swapped with the last. In fact, I think it forces all card to be swapped
"downward" only if their number doesn't come up before they are reached. Or
something.
I think I prefer swapping each location with a candidate location of the
entire group for each swap.
This is the way the shuffle algorithm in Java is coded, and I was just
asking if anyone here knew the source for std::shuffle in C++.
-- Gary
- Previous message: Alwyn: "Re: ios::failbit always true"
- In reply to: David White: "Re: Random numbers with no duplicates"
- Next in thread: Spacen Jasset: "Re: Random numbers with no duplicates"
- Reply: Spacen Jasset: "Re: Random numbers with no duplicates"
- Reply: Francis Glassborow: "Re: Random numbers with no duplicates"
- Reply: David White: "Re: Random numbers with no duplicates"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|