Skip to content

Instantly share code, notes, and snippets.

@iandundas
Last active April 23, 2022 16:30
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iandundas/59303ab6fd443b5eec39 to your computer and use it in GitHub Desktop.
Save iandundas/59303ab6fd443b5eec39 to your computer and use it in GitHub Desktop.
Random Emoji
// Returns a random Emoji 🌿
extension String{
func randomEmoji()->String{
let range = 0x1F601...0x1F64F
let ascii = range.startIndex + Int(arc4random_uniform(UInt32(range.count)))
let emoji = String(UnicodeScalar(ascii))
return emoji
}
}
@oaleeapp
Copy link

// Thanks for inspiring,

    static func randomEmoji()->String{
        let emojiStart = 0x1F601
        let ascii = emojiStart + Int(arc4random_uniform(UInt32(35)))
        let emoji = UnicodeScalar(ascii)?.description
        return emoji ?? "x"
    }
}```

I modify a little to make it work on current version of Swift

@omardlhz
Copy link

omardlhz commented Apr 8, 2017

This is amazing!

@gavi
Copy link

gavi commented Jun 27, 2017

An update

extension String{
    static func randomEmoji()->String{
        let range = [UInt32](0x1F601...0x1F64F)
        let ascii = range[Int(drand48() * (Double(range.count)))]
        let emoji = UnicodeScalar(ascii)?.description
        return emoji!
    }
}

@mmuszynski
Copy link

It may be a bit more in line with current Swift syntax to make this a static variable instead. It's a small change, but here you go:

extension String{
    static var randomEmoji: String {
        let range = [UInt32](0x1F601...0x1F64F)
        let ascii = range[Int(drand48() * (Double(range.count)))]
        let emoji = UnicodeScalar(ascii)?.description
        return emoji!
    }
}

@cellularmitosis
Copy link

I adapted this for representing pointer addresses as emoji. Thanks! https://gist.github.com/cellularmitosis/d425aae5f1a2e5d9bfa1d4c1a5968d22

@kiler129
Copy link

Keep in mind this is NOT a definitive list of emojis: https://stackoverflow.com/questions/30470079/emoji-value-range

@pepasibble
Copy link

Heyo,

I've updated my approach to allow multiple discontiguous ranges of emojis. This allows you to make use of more emoji.

extension NSObject {
    public var asPointer: UnsafeMutableRawPointer {
        return Unmanaged.passUnretained(self).toOpaque()
    }
}


fileprivate let contiguousEmoji: [UnicodeScalar] = {
    let ranges: [ClosedRange<Int>] = [
        0x1f600...0x1f64f,
        0x1f680...0x1f6c5,
        0x1f6cb...0x1f6d2,
        0x1f6e0...0x1f6e5,
        0x1f6f3...0x1f6fa,
        0x1f7e0...0x1f7eb,
        0x1f90d...0x1f93a,
        0x1f93c...0x1f945,
        0x1f947...0x1f971,
        0x1f973...0x1f976,
        0x1f97a...0x1f9a2,
        0x1f9a5...0x1f9aa,
        0x1f9ae...0x1f9ca,
        0x1f9cd...0x1f9ff,
        0x1fa70...0x1fa73,
        0x1fa78...0x1fa7a,
        0x1fa80...0x1fa82,
        0x1fa90...0x1fa95,
    ]
    
    return ranges.reduce([], +).map { return UnicodeScalar($0)! }
}()


extension UnsafeMutableRawPointer {
    public var asEmoji: String {
        // Inspired by https://gist.github.com/iandundas/59303ab6fd443b5eec39
        let index = abs(self.hashValue) % contiguousEmoji.count
        return String(contiguousEmoji[index])
    }
}

usage:

foo.asPointer.asEmoji

https://gist.github.com/cellularmitosis/d425aae5f1a2e5d9bfa1d4c1a5968d22#gistcomment-3644352

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment