Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stack_of_cards = []
- stack_of_cards << Card.new("A", "Spades", 1)
- stack_of_cards << Card.new("2", "Spades", 2)
- stack_of_cards << Card.new("3", "Spades", 3)
- ...
- class Card
- attr_accessor :number, :suit, :value
- def initialize(number, suit, value)
- @number = number
- @suit = suit
- @value = value
- end
- def to_s
- "#{@number} of #{@suit}"
- end
- end
- stack_of_cards.shuffle
- stack_of_cards.shuffle!
- class Hash
- def shuffle
- Hash[self.to_a.sample(self.length)]
- end
- def shuffle!
- self.replace(self.shuffle)
- end
- end
- array.sort {|a, b| rand <=> rand }
- def shuffle_me(array)
- (array.size-1).downto(1) do |i|
- j = rand(i+1)
- array[i], array[j] = array[j], array[i]
- end
- array
- end
- array.shuffle
- [1, 3, 2].shuffle
- #=> [3, 1, 2]
- Hash[*hash.to_a.shuffle.flatten]
- Hash[*{a: 1, b: 2, c: 3}.to_a.shuffle.flatten(1)]
- #=> {:b=>2, :c=>3, :a=>1}
- #=> {:c=>3, :a=>1, :b=>2}
- #=> {:a=>1, :b=>2, :c=>3}
- # Also works for hashes containing arrays
- Hash[*{a: [1, 2], b: [2, 3], c: [3, 4]}.to_a.shuffle.flatten(1)]
- #=> {:b=>2, :c=>3, :a=>1}
- #=> {:c=>[3, 4], :a=>[1, 2], :b=>[2, 3]}
- a = {"a" => 1, "b" => 2, "c" => 3}
- puts a.inspect
- a = a.sort {|a, b| rand <=> rand }.to_h
- puts a.inspect
Add Comment
Please, Sign In to add comment