Research the Fisher-Yates shuffling algorithm online, then use it to reimplement the shuffle method in Fig.7.12. Fig.7.12
Question:
Research the Fisher-Yates shuffling algorithm online, then use it to reimplement the shuffle method in Fig.7.12.
Fig.7.12
Transcribed Image Text:
I // Fig. 7.12: DeckOfCards.java 2 // DeckOfCards class represents a deck of playing cards. 3 import java.security.SecureRandom; 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 public class DeckOfCards { 52 53 } // random number generator private static final SecureRandom randomNumbers = new SecureRandom(); private static final int NUMBER_OF_CARDS = 52; // constant # of Cards private Card [] deck = new Card [NUMBER_OF_CARDS]; // Card references private int currentCard = 0; // index of next Card to be dealt (0-51) // constructor fills deck of Cards public DeckOfCards () { String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"}; } } // populate deck with Card objects for (int count = 0; countdeck.length; count++) { deck [count] = new Card(faces [count % 13], suits [count / 13]); // shuffle deck of Cards with one-pass algorithm public void shuffle() { // next call to method dealCard should start at deck [0] again currentCard = 0; } } // for each Card, pick another random Card (0-51) and swap them for (int first = 0; first < deck. length; first++) { // select a random number between 0 and 51 int second = randomNumbers.nextInt (NUMBER_OF_CARDS); } // swap current Card with randomly selected Card Card temp = deck[first]; // deal one Card public Card dealCard() { // determine whether Cards remain to be dealt if (currentCard < deck. length) { return deck[currentCard++]; // return current Card in array deck[first] = deck[second]; deck[second] = temp; } } else { return null; // return null to indicate that all Cards were dealt
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 0% (1 review)
The FisherYates shuffle is an algorithm for generating a random permutation of a finite sequencein p...View the full answer
Answered By
Ankit Mahajan
I am an electrical engineering graduate from Thapar institute of engineering and technology.
Qualified exams - GATE 2019,2020.
CAT EXAM 2021- 91.4 percentile
SSC EXAMS- 2019,2020,2021
AFCAT EXAM- 2019,2020,2021
I want to share my knowledge with other people so that they can achieve the same.
I have strong hold Mathematics, Electrical engineering and all the subjects related.
Just give me a problem and I will give you the solution of it.
5.00+
1+ Reviews
10+ Question Solved
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Question Posted:
Students also viewed these Computer science questions
-
Write a literature review for your study. See below for an example of a literature review. Your literature review should provide both analysis and synthesis of previous studies as related to the...
-
In the following problems properties of the Laplace transform are used. (a) Show that the Laplace transform of x(t) e at u(t)is X(s + a), where X(s) = L[x(t)] and then use it to find the Laplace...
-
Implement a bogus sorting algorithm called bogo sort that uses your shuffling algorithm from the previous exercise to sort an array of numbers. The bogo sort algorithm is the following: Obviously,...
-
Elm Corp. makes two products: C and D. The following data have been summarized: Indirect manufacturing cost information includes the following: The company plans to manufacture 250 units of each...
-
Differentiate between an originating temporary difference and a reversing difference.
-
Match the description of the conic with its standard equation. The equations are labeled (a)-(f). a. (x - h)2 / a2 + (y - 5)2 / b2 = 1 b. (x - h)2 / a2 - (y - k)2 / b2 = 1 c. (y - 5)2 / a2 - (x - h)2...
-
Identify the four components of an ecosystem. After you do this try to visualize the interactions of these four components of an ecosystem as illustrated by Figure 2. 2 in the textbook. Does this...
-
The Caraway Seed Company grows heirloom tomatoes and sells their seeds. The heirloom tomato plants are preferred by many growers for their superior flavor. At the end of the most recent year the firm...
-
Solving via linear equation No unread replies.No replies. A small country exports soybeans and flowers. Soybeans require 8 workers per acre, flowers require 12 workers per acre, and 100,000 workers...
-
While exercising, you can use a heart-rate monitor to see that your heart rate stays within a safe range suggested by your trainers and doctors. According to the American Heart Association (AHA)...
-
Modify the application of Figs. 7.117.13 to use Face and Suit enum types to represent the faces and suits of the cards. Declare each of these enum types as a public type in its own source-code file....
-
Show by an example that every proper subgroup of a nonabelian group may be abelian.
-
In his book Forbidden Grounds, University of Chicago Professor Richard Epstein argues that federal employment antidiscrimination laws ought to be abolished. [Reading Westmont College economist Edd...
-
Other things being equal, explain why you think each of the following changes would increase, decrease, or have no effect on the width of a 95 percent confidence interval for the population mean: a....
-
In an article in the Journal of Human Resources titled The Economic Reality of the Beauty Myth, economists Susan Averett and Sanders Korenman found that family income of obese women is about 17...
-
John has taxable income of $30,000. William has taxable income of $60,000. Determine their 2017 income taxes if they are both single individuals and claim the standard deduction. Compare their...
-
Sally and Jim are married and have taxable income in 2017 of $160,000. If they could file their income tax as single individuals, each of them would have taxable income of $80,000. Do they have a...
-
Employee says she was forced to quit her job because of her status as a mother of young children. She claimed that her female supervisor created a hostile work environment that violated Title VII....
-
Grace is training to be an airplane pilot and must complete five days of flying training in October with at least one day of rest between trainings. How many ways can Grace schedule her flying...
-
The use of null values in a map is problematic, as there is then no way to differentiate whether a null value returned by the call get(k) represents the legitimate value of an entry (k,null), or...
-
A group of children want to play a game, called Unmonopoly, where in each turn the player with the most money must give half of his/her money to the player with the least amount of money. What data...
-
Assuming the input to the sorting problem is given in an array A, describe how to implement the insertion-sort algorithm using only the array A and at most six additional (base-type) variables.
-
what is the difference between being a naturalized Canadian citizen and a Canadian citizen by birth? Do both types of citizen have the same rights?
-
Constitution says that one of the powers of Congress is to "To establish an uniform Rule of Naturalization." This means Congress is responsible for passing laws with respect to immigration. Find a...
-
Why are immigration laws a big issue who does it affect and why? Why are the laws so strict?
Study smarter with the SolutionInn App