douju6542 2018-10-06 04:54
浏览 174
已采纳

在自定义范围内生成随机数

I have go script that starts on "localhost:8080/1" with a previous and next links I need to add Random link with custom range that I can change like:

  • small numbers like 100 to 200 "localhost:8080/100 - 200" and
  • even to big number like: "16567684686592643791596485465456223131545455682945955"

So:

// Get next and previous page numbers

previous := new(big.Int).Sub(page, one)
next := new(big.Int).Add(page, one)
random :=????
  • 写回答

1条回答 默认 最新

  • dongzhi9192 2018-10-06 05:06
    关注

    You need to use the package crypto.rand Int() function, which does support big.Int (as opposed to the math.rand package)

    See this article (and its playground example):

    package main
    
    import (
        "fmt"
        "math/big"
        "crypto/rand"
    )
    
    func main() {
        var prime1, _ = new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
        // Generate random numbers in range [0..prime1]
        // Ignore error values
        // Don't use this code to generate secret keys that protect important stuff!
        x, _ := rand.Int(rand.Reader, prime1)
        y, _ := rand.Int(rand.Reader, prime1)
        fmt.Printf("x: %v
    ", x)
        fmt.Printf("y: %v
    ", y)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝