Skip to content

Commit

Permalink
deprecated old shuffle, new shuffle added
Browse files Browse the repository at this point in the history
  • Loading branch information
gandhi-jay committed Dec 2, 2017
1 parent 217e685 commit 246cbca
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions go_stephen_grider/code/05_cards_shuffle/deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"os"
"strings"
"time"
)

// Create a new type of deck
Expand All @@ -23,7 +24,7 @@ func (d deck) print() {
func newDeck() deck {
cards := deck{}
cardSuits := []string{"Diamonds", "Spades", "Hearts", "Clubs"}
cardValues := []string{"Ace", "Two", "Three", "Four", "Five"}
cardValues := []string{"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}

for _, cardSuit := range cardSuits {
for _, cardValue := range cardValues {
Expand Down Expand Up @@ -58,9 +59,24 @@ func newDeckFromFile(filename string) deck {

}

func (d deck) shuffle() {
func (d deck) shuffleDeprecated() {
for index := range d {
// Go Random number generation works based on Seed
// every time seed is same, that's why it's not repeatiting randomize
// Based on same seed, generator gives same number of sequence.
newPos := rand.Intn(len(d) - 1)
d[index], d[newPos] = d[newPos], d[index]
}
}

func (d deck) shuffle() {
// Creating new Source for generator based on timestamp
source := rand.NewSource(time.Now().UnixNano())
// Giving a generator a new source.
r := rand.New(source)
for index := range d {
// Based on generator on new seed.
newPos := r.Intn(len(d) - 1)
d[index], d[newPos] = d[newPos], d[index]
}
}

0 comments on commit 246cbca

Please sign in to comment.