随机字母数字字符串Linux Swift 3 [英] Random Alphanumeric String Linux Swift 3

查看:111
本文介绍了随机字母数字字符串Linux Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中使用Swift 3尝试生成随机字符串时遇到两个问题.

Im having 2 problems when trying to generate a random string in Linux with Swift 3.

  1. arc4random_uniform仅在BSD上的Linux中不可用.所以我能够摆脱使用random()函数.这在我生成可变大小的随机数时有效(请参见下面的代码)

  1. arc4random_uniform is not available in Linux only on BSD. SO i was able to get away with using random() function. And this worked when i was generating random numbers of a variable size (See code below)

func generateRandomNumber() -> Int
{
   var place = 1

   var finalNumber = 0;

#if os(Linux)
for _ in 0..<5
{
    place *= 10

    let randomNumber = Int(random() % 10) + 1

    finalNumber += randomNumber * place
}
#else
for _ in 0..<5
{
    place *= 10

    let randomNumber = Int(arc4random_uniform(10))

    finalNumber += randomNumber * place
}
#endif

  return finalNumber
}

那行得通. ,但是每次都给我相同的号码:(

And that WORKS. it works but it gives me the same number every time :(

  1. 当尝试生成随机字母数字字符串时,我仅限于使用Swift String和NOT NSSTRING. Linux抛出此错误

Linux之前的原始代码块:

original pre Linux block of code:

   func randomString(_ length: Int) -> String
   {

      let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
      let len = UInt32(letters.length)

      var randomString = ""

      for _ in 0 ..< length {
    let rand = arc4random_uniform(len)
    var nextChar = letters.character(at: Int(rand))
    randomString += NSString(characters: &nextChar, length: 1) as String
      }

       return randomString
    }

以及使用上述代码时得到的实际错误

And the actual error I get when using above code

    error: cannot convert value of type 'NSString' to type 'String' in coercion
        randomString += NSString(characters: &nextChar, length: 1) as String

针对linux代码块进行了修改.

modified for linux block of code.

    func randomString(_ length: Int) -> String
    {

let letters : String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let len = letters.characters.count

var randomString = ""

#if os(Linux)

    for _ in 0..<length
    {
        let randomValue = (random() % len) + 1

        randomString += "\(letters[letters.index(letters.startIndex, offsetBy: Int(randomValue))])"
    }

    #else
    for _ in 0 ..< length
    {
        let rand = arc4random_uniform(UInt32(len))

        randomString += "\(letters[letters.index(letters.startIndex, offsetBy: Int(rand))])"
    }
    #endif


      return randomString
}          

但是这次错误很奇怪,它只显示非法指令,没有其他信息.我在交互模式下运行docker容器,并且在调用其他函数等时看到服务器正在运行并打印出来.

but this time the error is weird it only says Illegal instruction with no extra information. I ran the docker container in interactive mode and i saw my server running and printing out when calling other functions etc.

但是问题是当我在IBM的swift中快速运行该功能时 沙盒

but the thing is the function actually WORKS when i ran it in IBMs swift sandbox

并且我假设它也使用linux.我非常困惑和困惑,任何帮助将不胜感激.

and I'm assuming its using linux also. Im very stuck and confused any help would be greatly appreciated.

( UPDATE ):我在带有单个swift文件而不是Vapor swift Web框架的linux env中运行了相同的功能.它的工作原理.就像我上面的编辑中提到的那样,它每次都会给我相同的随机字符串.构建完成后,我仍然必须测试整个项目.但是除此之外,我还需要知道random()函数是否每次实际上都会给我一些新的东西,而不是相同的废话.

(UPDATE): I ran the same function in just a linux env with a single swift file and not the Vapor swift web framework. and it works. As mentioned in my edit above it gives me the same random string everytime. I will still have to test the entire project once my build finishes. But besides that i need to know if the random() function will actually give me something new each time instead of the same crap.

推荐答案

弄清楚了.

所以重复的随机数/字符串的答案是在我调用random()函数之前就添加这行

So the answer to the repeating random number/string was to just add this line before i called the random() function

srand(UInt32(time(nil)))

并且我假设这也是修复非法指令的原因.因为我不记得要更改任何其他内容.

and I'm assuming thats what fixed the illegal instruction also. Because i don't recall changing anything else.

不用说这是我的最终结果

Needless to say here is my final result

 func generateRandomNumber() -> Int
 {
    var place = 1

    var finalNumber = 0;

    #if os(Linux)

    srand(UInt32(time(nil)))

    for _ in 0..<5
    {
        place *= 10

        let randomNumber = Int(random() % 10) + 1

        finalNumber += randomNumber * place
    }
    #else
    for _ in 0..<5
    {
        place *= 10

        let randomNumber = Int(arc4random_uniform(10))

        finalNumber += randomNumber * place
    }
    #endif

     return finalNumber
 }



 func randomString(_ length: Int) -> String
 {

    let letters : String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    let len = letters.characters.count

    var randomString = ""

    #if os(Linux)

    srand(UInt32(time(nil)))

   for _ in 0..<length
   {
     let randomValue = (random() % len) + 1

     randomString += "\(letters[letters.index(letters.startIndex, offsetBy: Int(randomValue))])"
   }

   #else
  for _ in 0 ..< length
  {
     let rand = arc4random_uniform(UInt32(len))

     randomString += "\(letters[letters.index(letters.startIndex, offsetBy: Int(rand))])"
  }
  #endif

   return randomString
 }  

这篇关于随机字母数字字符串Linux Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆