Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I recentrly started to work with swift, and me and my Mate are trying to make a Small game to test what we learned..
we made a sort of monster fighting game ,no moving object just clicking on the monster.
So far so good evrything works as it should, got Upgrade to get more Hp or Str to do more damage on the monster, and to get more Hp when it hits back.
that said. i'm made items to the game but i want them to drop by the monster.
made different types and would like to know if anyone knows a code to get a Drop Chance on the Items...
like
"Normal" = 73%
"Rare" = 25%
"Legend" = 2%
I Looked around on evry search engine but can't find anything that i need.
Hope to get a reply soon
Thanks.
Greetz Kristof.V
This logic is the same of the answer by Avt, just a different implementation.
let random = Int(arc4random_uniform(100))
switch random {
case 0...72: print("Normal")
case 73...97: print("Rare")
case 98...99: print("Legend")
default: fatalError("Wrong number")
}
Use a random function arc4random_uniform
Returns a random number between 0 and the inserted parameter minus 1.
For example arc4random_uniform(3) may return 0, 1 or 2 but not 3.
You can use it like:
let random = Int(arc4random_uniform(100))
if random < 73 {
// drop normal
}
else if random < 73+25 {
// drop rare
} else {
// drop legend
}
(Edited: to include also weapon type as per follow-up question Random number to print array (Swift))
As above, use the arc4random function. A more complete "Monster" example follows.
It could be appropriate to hold different rarity and weapon types as enum cases
enum Rarities {
case Normal
case Rare
case Legendary
}
enum Weapons {
case Knife
case Sword
case Katana
}
Each monster could be an instance of a Monster class with it's own specific item and rarity drop rates, initialized when creating the Monster object.
class Monster {
var name : String
var dropRatesRarity = [Rarities:Double]()
var dropRatesWeapons = [Weapons:Double]()
init(name: String, dropRatesRarity: [Rarities:Double], dropRatesWeapons: [Weapons:Double]) {
self.name = name
var rateSum = dropRatesRarity.values.reduce(0.0, combine: +)
var dropRatesCumSum = 0.0
for (k, v) in dropRatesRarity {
self.dropRatesRarity[k] = v/rateSum + dropRatesCumSum
dropRatesCumSum += v/rateSum
}
rateSum = dropRatesWeapons.values.reduce(0.0, combine: +)
dropRatesCumSum = 0.0
for (k, v) in dropRatesWeapons {
self.dropRatesWeapons[k] = v/rateSum + dropRatesCumSum
dropRatesCumSum += v/rateSum
}
}
func dropItem() -> (Weapons,Rarities) {
return (generateItemType(), generateRarity())
}
func generateRarity() -> Rarities {
let random = Double(Float(arc4random()) / Float(UINT32_MAX))
return dropRatesRarity.filter({ (k, v) in v >= random }).minElement({ $0.1 < $1.1 })?.0 ?? .Normal
}
func generateItemType() -> Weapons {
let random = Double(Float(arc4random()) / Float(UINT32_MAX))
return dropRatesWeapons.filter({ (k, v) in v >= random }).minElement({ $0.1 < $1.1 })?.0 ?? .Knife
}
}
Example for some Monster instance:
/* Example */
var myDropRatesRarity = [Rarities:Double]()
myDropRatesRarity[.Normal] = 73 // relative drop rates, needn't sum to 100
myDropRatesRarity[.Rare] = 25
myDropRatesRarity[.Legendary] = 2
var myDropRatesWeapons = [Weapons:Double]()
myDropRatesWeapons[.Knife] = 50
myDropRatesWeapons[.Sword] = 30
myDropRatesWeapons[.Katana] = 20
var myMonster = Monster(name: "Godzilla", dropRatesRarity: myDropRatesRarity, dropRatesWeapons: myDropRatesWeapons)
var myItem = myMonster.dropItem()
print(myMonster.name + " dropped a \(myItem.0) of \(myItem.1) rarity!")
/* "Godzilla dropped a Katana of Normal rarity!" */
/* ... most likely Normal rarity ... keep grinding! */
Related
I am pretty Newbie to programming. And I am trying to pile up the random blocks dynamically till it hits the upper frame. But it seems that Swift doesn't let me to do so. Did I miss anything please? Any input are appreciated.
let blocks =[block1,block2,block3,block4,block5,block6,block7,block8,block9,block10,block11,block12]
var block:SKSpriteNode!
let blockX:Double = 0.0
var blockY:Double = -(self.size.height/2)
repeat{
block = blocks.randomBlock()
block.zPosition = 2
block.position = CGPoint(x:blockX, y:blockY)
block.size.height = 50
block.size.width = 50
self.addChild(block)
blockY += 50
} while( block.position.y < self.size.height)
extension Array {
func randomBlock()-> Element {
let randint = Int(arc4random_uniform(UInt32(self.count)))
return self[randint]
}
}
you need to have someway of tracking which blocks have been selected and ensure that they don't get selected again. The method below uses an array to store the indexes of selected blocks and then uses recursion to find a cycle through until an unused match is found.
private var usedBlocks = [Int]()
func randomBlock() -> Int {
guard usedBlocks.count != blocks.count else { return -1 }
let random = Int(arc4random_uniform(UInt32(blocks.count)))
if usedBlocks.contains(random) {
return randomBlock()
}
usedBlocks.append(random)
return random
}
in your loop change your initializer to
let index = randomBlock()
if index > -1 {
block = blocks[index]
block.zPosition = 2
block.position = CGPoint(x:blockX, y:blockY)
}
remember that if you restart the game or start a new level, etc. you must clear all of the objects from usedBlocks
usedBlocks.removeAll()
I have a function in Swift that computes the hamming distance of two strings and then puts them into a connected graph if the result is 1.
For example, read to hear returns a hamming distance of 2 because read[0] != hear[0] and read[3] != hear[3].
At first, I thought my function was taking a long time because of the quantity of input (8,000+ word dictionary), but I knew that several minutes was too long. So, I rewrote my same algorithm in Java, and the computation took merely 0.3s.
I have tried writing this in Swift two different ways:
Way 1 - Substrings
extension String {
subscript (i: Int) -> String {
return self[Range(i ..< i + 1)]
}
}
private func getHammingDistance(w1: String, w2: String) -> Int {
if w1.length != w2.length { return -1 }
var counter = 0
for i in 0 ..< w1.length {
if w1[i] != w2[i] { counter += 1 }
}
return counter
}
Results: 434 seconds
Way 2 - Removing Characters
private func getHammingDistance(w1: String, w2: String) -> Int {
if w1.length != w2.length { return -1 }
var counter = 0
var c1 = w1, c2 = w2 // need to mutate
let length = w1.length
for i in 0 ..< length {
if c1.removeFirst() != c2.removeFirst() { counter += 1 }
}
return counter
}
Results: 156 seconds
Same Thing in Java
Results: 0.3 seconds
Where it's being called
var graph: Graph
func connectData() {
let verticies = graph.canvas // canvas is Array<Node>
// Node has key that holds the String
for vertex in 0 ..< verticies.count {
for compare in vertex + 1 ..< verticies.count {
if getHammingDistance(w1: verticies[vertex].key!, w2: verticies[compare].key!) == 1 {
graph.addEdge(source: verticies[vertex], neighbor: verticies[compare])
}
}
}
}
156 seconds is still far too inefficient for me. What is the absolute most efficient way of comparing characters in Swift? Is there a possible workaround for computing hamming distance that involves not comparing characters?
Edit
Edit 1: I am taking an entire dictionary of 4 and 5 letter words and creating a connected graph where the edges indicate a hamming distance of 1. Therefore, I am comparing 8,000+ words to each other to generate edges.
Edit 2: Added method call.
Unless you chose a fixed length character model for your strings, methods and properties such as .count and .characters will have a complexity of O(n) or at best O(n/2) (where n is the string length). If you were to store your data in an array of character (e.g. [Character] ), your functions would perform much better.
You can also combine the whole calculation in a single pass using the zip() function
let hammingDistance = zip(word1.characters,word2.characters)
.filter{$0 != $1}.count
but that still requires going through all characters of every word pair.
...
Given that you're only looking for Hamming distances of 1, there is a faster way to get to all the unique pairs of words:
The strategy is to group words by the 4 (or 5) patterns that correspond to one "missing" letter. Each of these pattern groups defines a smaller scope for word pairs because words in different groups would be at a distance other than 1.
Each word will belong to as many groups as its character count.
For example :
"hear" will be part of the pattern groups:
"*ear", "h*ar", "he*r" and "hea*".
Any other word that would correspond to one of these 4 pattern groups would be at a Hamming distance of 1 from "hear".
Here is how this can be implemented:
// Test data 8500 words of 4-5 characters ...
var seenWords = Set<String>()
var allWords = try! String(contentsOfFile: "/usr/share/dict/words")
.lowercased()
.components(separatedBy:"\n")
.filter{$0.characters.count == 4 || $0.characters.count == 5}
.filter{seenWords.insert($0).inserted}
.enumerated().filter{$0.0 < 8500}.map{$1}
// Compute patterns for a Hamming distance of 1
// Replace each letter position with "*" to create patterns of
// one "non-matching" letter
public func wordH1Patterns(_ aWord:String) -> [String]
{
var result : [String] = []
let fullWord : [Character] = aWord.characters.map{$0}
for index in 0..<fullWord.count
{
var pattern = fullWord
pattern[index] = "*"
result.append(String(pattern))
}
return result
}
// Group words around matching patterns
// and add unique pairs from each group
func addHamming1Edges()
{
// Prepare pattern groups ...
//
var patternIndex:[String:Int] = [:]
var hamming1Groups:[[String]] = []
for word in allWords
{
for pattern in wordH1Patterns(word)
{
if let index = patternIndex[pattern]
{
hamming1Groups[index].append(word)
}
else
{
let index = hamming1Groups.count
patternIndex[pattern] = index
hamming1Groups.append([word])
}
}
}
// add edge nodes ...
//
for h1Group in hamming1Groups
{
for (index,sourceWord) in h1Group.dropLast(1).enumerated()
{
for targetIndex in index+1..<h1Group.count
{ addEdge(source:sourceWord, neighbour:h1Group[targetIndex]) }
}
}
}
On my 2012 MacBook Pro, the 8500 words go through 22817 (unique) edge pairs in 0.12 sec.
[EDIT] to illustrate my first point, I made a "brute force" algorithm using arrays of characters instead of Strings :
let wordArrays = allWords.map{Array($0.unicodeScalars)}
for i in 0..<wordArrays.count-1
{
let word1 = wordArrays[i]
for j in i+1..<wordArrays.count
{
let word2 = wordArrays[j]
if word1.count != word2.count { continue }
var distance = 0
for c in 0..<word1.count
{
if word1[c] == word2[c] { continue }
distance += 1
if distance > 1 { break }
}
if distance == 1
{ addEdge(source:allWords[i], neighbour:allWords[j]) }
}
}
This goes through the unique pairs in 0.27 sec. The reason for the speed difference is the internal model of Swift Strings which is not actually an array of equal length elements (characters) but rather a chain of varying length encoded characters (similar to the UTF model where special bytes indicate that the following 2 or 3 bytes are part of a single character. There is no simple Base+Displacement indexing of such a structure which must always be iterated from the beginning to get to the Nth element.
Note that I used unicodeScalars instead of Character because they are 16 bit fixed length representations of characters that allow a direct binary comparison. The Character type isn't as straightforward and take longer to compare.
Try this:
extension String {
func hammingDistance(to other: String) -> Int? {
guard self.characters.count == other.characters.count else { return nil }
return zip(self.characters, other.characters).reduce(0) { distance, chars in
distance + (chars.0 == chars.1 ? 0 : 1)
}
}
}
print("read".hammingDistance(to: "hear")) // => 2
The following code executed in 0.07 secounds for 8500 characters:
func getHammingDistance(w1: String, w2: String) -> Int {
if w1.characters.count != w2.characters.count {
return -1
}
let arr1 = Array(w1.characters)
let arr2 = Array(w2.characters)
var counter = 0
for i in 0 ..< arr1.count {
if arr1[i] != arr2[i] { counter += 1 }
}
return counter
}
After some messing around, I found a faster solution to #Alexander's answer (and my previous broken answer)
extension String {
func hammingDistance(to other: String) -> Int? {
guard !self.isEmpty, !other.isEmpty, self.characters.count == other.characters.count else {
return nil
}
var w1Iterator = self.characters.makeIterator()
var w2Iterator = other.characters.makeIterator()
var distance = 0;
while let w1Char = w1Iterator.next(), let w2Char = w2Iterator.next() {
distance += (w1Char != w2Char) ? 1 : 0
}
return distance
}
}
For comparing strings with a million characters, on my machine it's 1.078 sec compared to 1.220 sec, so roughly a 10% improvement. My guess is this is due to avoiding .zip and the slight overhead of .reduce and tuples
As others have noted, calling .characters repeatedly takes time. If you convert all of the strings once, it should help.
func connectData() {
let verticies = graph.canvas // canvas is Array<Node>
// Node has key that holds the String
// Convert all of the keys to utf16, and keep them
let nodesAsUTF = verticies.map { $0.key!.utf16 }
for vertex in 0 ..< verticies.count {
for compare in vertex + 1 ..< verticies.count {
if getHammingDistance(w1: nodesAsUTF[vertex], w2: nodesAsUTF[compare]) == 1 {
graph.addEdge(source: verticies[vertex], neighbor: verticies[compare])
}
}
}
}
// Calculate the hamming distance of two UTF16 views
func getHammingDistance(w1: String.UTF16View, w2: String.UTF16View) -> Int {
if w1.count != w2.count {
return -1
}
var counter = 0
for i in w1.startIndex ..< w1.endIndex {
if w1[i] != w1[i] {
counter += 1
}
}
return counter
}
I used UTF16, but you might want to try UTF8 depending on the data. Since I don't have the dictionary you are using, please let me know the result!
*broken*, see new answer
My approach:
private func getHammingDistance(w1: String, w2: String) -> Int {
guard w1.characters.count == w2.characters.count else {
return -1
}
let countArray: Int = w1.characters.indices
.reduce(0, {$0 + (w1[$1] == w2[$1] ? 0 : 1)})
return countArray
}
comparing 2 strings of 10,000 random characters took 0.31 seconds
To expand a bit: it should only require one iteration through the strings, adding as it goes.
Also it's way more concise 🙂.
i have some question about swift 2 random. I have an enum sub class of all cards example:
enum CardName : Int{
case Card2Heart = 0,
Card2Diamond,
Card2Club,
Card2Spade,
Card3Heart..... }
I want to select 10 random cards on the didMoveToView
To get a unique, random set of numbers you can do the following...
Using the Fisher-Yates shuffle from here... How do I shuffle an array in Swift?
You can do...
var numbers = Array(0...51)
numbers.shuffleInPlace()
let uniqueSelection = numbers[0..<10]
or...
let uniqueSelection = Array(0...51).shuffleInPlace()[0..<10]
This will create a random, unique selection of 10 numbers (cards) from the array of 52 cards that you start with.
You can then iterate this array to get the enums or create an array of all enums to start from etc... There are lots of ways to use this.
In Swift 4.2 (coming with Xcode 10) the task will become much easier:
enum CardName: CaseIterable {
case Card2Heart
case Card2Diamond
case Card2Club
case Card2Spade
case Card3Heart
// ...
}
let randomCard = CardName.allCases.randomElement()
print(randomCard)
let randomCards10 = CardName.allCases.shuffled().prefix(10)
print(randomCards10)
Note there is no need for the enum to inherit from Int.
Following your last comment, here's a little, simplified example with the constraint of having to keep your enum for making the cards.
We need to include the extensions linked by Fogmeister:
extension MutableCollectionType where Index == Int {
/// Shuffle the elements of `self` in-place.
mutating func shuffleInPlace() {
// empty and single-element collections don't shuffle
if count < 2 { return }
for i in 0..<count - 1 {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
guard i != j else { continue }
swap(&self[i], &self[j])
}
}
}
extension CollectionType {
/// Return a copy of `self` with its elements shuffled
func shuffle() -> [Generator.Element] {
var list = Array(self)
list.shuffleInPlace()
return list
}
}
These extensions will allow us to shuffle an array of values.
Which array?
There's many ways, but the simplest option is probably to make an array of indices, which are simple integers (replace 52 with the actual number of cards in your enum):
Array(1...52) // [1, 2, 3, ... , 52]
We shuffle it:
Array(1...52).shuffle() // [33, 42, 7, ...]
Now we have an array of randomized indices. Let's make cards from this with your enum:
Array(0...51).shuffle().flatMap({ CardName(rawValue: $0) })
This is it, we have an array of cards in a random order:
let shuffledDeck = Array(0...51).shuffle().flatMap({ CardName(rawValue: $0) }) // [Card3Heart, Card2Diamond, ...]
and we can take cards from it:
func takeCardsFromDeck(number: Int) -> [CardName] {
if shuffledDeck.count > number {
let cards = Array(shuffledDeck[0..<number])
shuffledDeck.removeRange(0..<number)
return cards
}
return []
}
let tenRandomCards = takeCards(10)
Of course we need to remove from the deck the cards we've dealt, that way each card you draw is unique: we're using removeRange for that.
This example was kept simple on purpose: you still have to verify that there's enough cards in the deck before drawing, and lots of unsuspected other complexities. But it's so fun. ;)
If you want, you can search for additional inspiration in my implementation of these models and others (Deck, Dealer, Player, etc) in my PokerHands repository (MIT Licenced) on GitHub.
Swift 4.2
No need for these extensions anymore, we can use the .shuffle() and .shuffled() methods provided by Swift. Just remove the extensions, and rename the methods: the equivalent of our old "shuffleInPlace" is now .shuffle() and the equivalent of our old "shuffle" is now .shuffled().
Note: see Sulthan's answer for an even better solution using Swift 4.2.
Here is the shuffleInPlace() code that you are missing;
extension MutableCollectionType where Index == Int {
mutating func shuffleInPlace() {
if count < 2 { return }
for i in 0..<count - 1 {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
guard i != j else { continue }
swap(&self[i], &self[j])
}
}
}
how to randomly spread enum values set
import Darwin // arc4random_uniform
enum E:Int {
case E1, E2, E3, E4, E5, E6, E7, E8, E9, E10
static var set:[E] { return (E.E1.rawValue...E.E10.rawValue).flatMap { E(rawValue: $0) }}
}
func spread(i:Int = 0, arr:[E])->([E],[E]) {
var i = i == 0 ? arr.count : i
var e:[E] = []
var arr = arr
while i > 0 && arr.count > 0 {
let idx = Int(arc4random_uniform(UInt32(arr.count-1)))
e.append(arr.removeAtIndex(idx))
i -= 1
}
return (e,arr)
}
let e1 = spread(3, arr: E.set)
let e2 = spread(2, arr: e1.1)
// ... spread the rest
let e3 = spread(arr: e2.1)
print(e1, e2, e3, separator:"\n")
/*
([E.E8, E.E6, E.E4], [E.E1, E.E2, E.E3, E.E5, E.E7, E.E9, E.E10])
([E.E1, E.E7], [E.E2, E.E3, E.E5, E.E9, E.E10])
([E.E5, E.E3, E.E2, E.E9, E.E10], [])
*/
I'm needing to create a random bool value in my game, in Swift.
It's basically, if Yes (or 1), spawn one object, if No (or 0), spawn the other.
So far, looking at this question and a similar one on here, I found this:
let randomSequenceNumber = Int(arc4random_uniform(2))
Now it works, but it seems bias to 0 to me... like ridiculously bias...
This is how I'm then using the value:
if(randomSequenceNumber == 0)
//spawn object A
else
//spawn object B
Is there a better way to implement this using a random bool value? That isn't bias to a certain value?
Update
Bit of an experiment to see how many 1's vs 0's were were generated in 10,000 calls:
func experiment() {
var numbers: [Int] = []
var tester: Int = 0
var sum = 0
for i in 0...10000 {
tester = Int(arc4random_uniform(2))
numbers.append(tester)
print(i)
}
for number in numbers {
sum += number
}
print("Total 1's: ", sum)
}
Test 1: Console Output: Total 1's: 4936
Test 2: Console Output: Total 1's: 4994
Test 3: Console Output: Total 1's: 4995
Xcode 10 with Swift 4.2
Looks like Apple  engineers are listening
let randomBool = Bool.random()
import Foundation
func randomBool() -> Bool {
return arc4random_uniform(2) == 0
}
for i in 0...10 {
print(randomBool())
}
for more advanced generator the theory is available here
for basic understanding of Bernoulli (or binomial) distribution check here
extension Bool {
static func random() -> Bool {
return arc4random_uniform(2) == 0
}
}
// usage:
Bool.random()
I want to increase each key and value in this dictionary by 0.5
var times = [0800.0:0855.0,0900.0:0939.0,0942.0:1021.0,1023.0:1054.0,1057.0:1136.0]
I'd recommend you to formulate a more elaborated question the next time.
What did you tried?. Where did you get stuck?.
A good startpoint is this guide.
Having said that, and asking what I think was your question:
The naive way of doing it is by iterating over the values and keys, increment the value/key and then remove the previous key:
for (k, v) in times {
times[k + 0.5] = v + 0.5
times[k] = nil
}
The problem with this approach is that if k + 0.5 exists in the dictionary at any point, the value will be overwritten.
Option B
var newTimes = [Double: Double]()
for (k, v) in times {
newTimes[k + 0.5] = v + 0.5
}
times = newTimes
If I understood your request better than how you put it, you are using the wrong data structure. You have a list of time ranges, not values of time associated to other values of time.
So, my suggestion is that first of all, instead of [Double: Double] you use a [ClosedInterval<Double>]. It's also cooler to define it, like let times = [0800.0 ... 0855.0, 0900.0 ... 0939.0].
Then, from there, it's quite easy to do what you requested:
let shiftedTimes = times.map { ($0.start + 0.5) ... ($0.end + 0.5) }
n.b.: The thing here is, always find the most suited data structure to the data. And most suited isn't the one I know better, but the one that represents it better.
EDIT
By improving on the solution, I'd say that the preferred type would be [ClosedInterval<NSTimeInterval>], that you can define operators like this:
func +<T: IntegerArithmeticType>(lhs: ClosedInterval<T>, rhs: T) -> ClosedInterval<T> {
return (lhs.start + rhs) ... (lhs.end + rhs)
}
func +<S: Strideable, T where S.Stride == T>(lhs: ClosedInterval<S>, rhs: T) -> ClosedInterval<S> {
return (lhs.start + rhs) ... (lhs.end + rhs)
}
and then the whole operation becomes let shiftedTimes = times.map { $0 + 0.5 } :)