Monday, February 15, 2010

How To Implement My Own Java Certificate Authority Java Code. Shuffling A "deck Of Cards"?

Java Code. Shuffling a "deck of cards"? - how to implement my own java certificate authority

Hello, I'm working is a card game, and I must follow the guidelines:
Look at the algorithm, which identifies a series of numbers, must be adapted and applied to shuffle the cards to start a new solitaire game. This algorithm is known as the Fisher-Yates algorithm was developed for computer Durst Field, Richard and popularized by Donald Knuth.

1. A [0], a [1] ... A n [-1] is an array of integers
2. N = N-1
3. Choose a random index k between 0 and N (inclusive).
4. Exchange values of A [k] and [N].
5. N loss after another.
6. Repeat step (c) to N is less than 1.

To select a random index (step nextInt c) the method of the Random class in the Java library. (Note: READ description of the method nextInt careful!)

I think I followed most of them, but I do not know how to implement a shuffle nextInt with () command. I somehow generate long by a parameter of type to be used as seeds, mix the random number generator to the cards used.

Currently, my code is as follows:
Closed public Shuffle (original cover)
(
int count = 0;
int temp;
Shuffle the deck = new Deck ();

while (count \\ \\ \\ \\ \\ \\ \\ \\ u0026lt; 52)
(
temp = (int) Math.random () * 52;
if (original.deck [temp] = null)
(
shuffle.deck [count]. original.deck range = [] temp rank;
shuffle.deck [count]. original.deck costume = [] temp costumes;
count + +;
)
)

Retushuffle RN;
)

Please help! Thank you!

2 comments:

BobaFett said...

If no argument constructor for java.util.Random in the JDK you use, you can "System.currentTimeMillis ()" parameter, as long as the seed used.

For the next random number from the generator in the range 0-51, inclusive, simple, "nextInt (52).

eg

import java.util.Random;

long seed = System.currentTimeMillis ();
Random random = new Random (seed);
int i = random.nextInt (52);

Michael from UK said...

Very good - I guess the bridge is initialized before you call that?

However, observations of Math.random (I assume that most pseudo-random numbers work the same), because they, at random numbers in the order indicated the same tendency to give the initial value.

Thus, instead of the production with Math.random () in the loop, you need the seeds initialized before the loop and pass as a parameter when the interior.

This will ensure a new sequence each time, thus preventing the shuffle, so that the same cards each time. It is typical for a time (or I / O function) Source Math.random the first call, for example, the time in milliseconds, as always when Math.random not offer a new way to sequence each time. This means that you cannextInt random method instead of "restart" sequence every time.

Edit
****
In terms of documentation, using each new Random object to initialize the default time for the seeds to create a chance in the loop, and within nextInt select the next random number.

Post a Comment