GVKun编程网logo

Python-生成具有给定(数字)分布的随机数(python生成指定数量的随机数)

15

对于想了解Python-生成具有给定的读者,本文将是一篇不可错过的文章,我们将详细介绍数字分布的随机数,并且为您提供关于c–具有Beta分布的随机数发生器、iphone–在Objective-C中生成

对于想了解Python-生成具有给定的读者,本文将是一篇不可错过的文章,我们将详细介绍数字分布的随机数,并且为您提供关于c – 具有Beta分布的随机数发生器、iphone – 在Objective-C中生成一个随机数字长度的随机数、Python – 具有有限十进制数字的随机浮点数、Python 中来自 PERT 分布的随机值的有价值信息。

本文目录一览:

Python-生成具有给定(数字)分布的随机数(python生成指定数量的随机数)

Python-生成具有给定(数字)分布的随机数(python生成指定数量的随机数)

如何解决Python-生成具有给定(数字)分布的随机数?

scipy.stats.rv_discrete可能就是你想要的。你可以通过values参数提供概率。然后,你可以使用rvs()分发对象的方法来生成随机数。

正如Eugene Pakhomov在评论中指出的那样,你还可以将p关键字参数传递给numpy.random.choice(),例如

numpy.random.choice(numpy.arange(1, 7), p=[0.1, 0.05, 0.05, 0.2, 0.4, 0.2])

从Python 3.6开始,Python的标准库中提供了一个解决方案random.choices

用法示例:让我们设置与OP的问题相匹配的总体和权重:

>>> from random import choices
>>> population = [1, 2, 3, 4, 5, 6]
>>> weights = [0.1, 0.05, 0.05, 0.2, 0.4, 0.2]
现在choices(population, weights)生成一个样本:

>>> choices(population, weights)
4

可选的仅关键字参数k允许一个参数一次请求多个样本。这很有价值,因为random.choices在生成任何样本之前,每次调用时都要做一些准备工作。通过一次生成许多样本,我们只需要做一次准备工作。在这里,我们生成了一百万个样本,并collections.Counter用来检查我们得到的分布与我们给出的权重大致匹配。

>>> million_samples = choices(population, weights, k=10**6)
>>> from collections import Counter
>>> Counter(million_samples)
Counter({5: 399616, 6: 200387, 4: 200117, 1: 99636, 3: 50219, 2: 50025})

解决方法

我有一个具有不同值的概率的文件,例如:

1 0.1
2 0.05
3 0.05
4 0.2
5 0.4
6 0.2

我想使用此分布生成随机数。是否存在处理此问题的现有模块?自己编写代码是很简单的(构建累积密度函数,生成随机值[0,1]并选择相应的值),但似乎这应该是一个常见问题,并且可能有人为它创建了一个函数/模块它。

我需要这个,因为我想生成一个生日列表(不遵循标准random模块中的任何分布)。

c – 具有Beta分布的随机数发生器

c – 具有Beta分布的随机数发生器

我需要像betarand(a,b)这样的函数的c或c源代码,它产生带有beta分布的随机数.我知道我可以使用boost库但是我要将它移植到CUDA架构中,所以我需要代码.有人能帮助我吗?
同时我有betapdf(Beta概率密度函数).但我不知道如何使用它来创建随机数:).

解决方法

C 11随机数库不提供beta分布.但是,beta分布可以根据库提供的两个gamma分布来建模.我已经为你实现了一个 beta_distribution的std :: gamma_distribution.据我所知,它完全符合随机数分布的要求.
#include <iostream>
#include <sstream>
#include <string>
#include <random>

namespace sftrabbit {

  template <typename RealType = double>
  class beta_distribution
  {
    public:
      typedef RealType result_type;

      class param_type
      {
        public:
          typedef beta_distribution distribution_type;

          explicit param_type(RealType a = 2.0,RealType b = 2.0)
            : a_param(a),b_param(b) { }

          RealType a() const { return a_param; }
          RealType b() const { return b_param; }

          bool operator==(const param_type& other) const
          {
            return (a_param == other.a_param &&
                    b_param == other.b_param);
          }

          bool operator!=(const param_type& other) const
          {
            return !(*this == other);
          }

        private:
          RealType a_param,b_param;
      };

      explicit beta_distribution(RealType a = 2.0,RealType b = 2.0)
        : a_gamma(a),b_gamma(b) { }
      explicit beta_distribution(const param_type& param)
        : a_gamma(param.a()),b_gamma(param.b()) { }

      void reset() { }

      param_type param() const
      {
        return param_type(a(),b());
      }

      void param(const param_type& param)
      {
        a_gamma = gamma_dist_type(param.a());
        b_gamma = gamma_dist_type(param.b());
      }

      template <typename URNG>
      result_type operator()(URNG& engine)
      {
        return generate(engine,a_gamma,b_gamma);
      }

      template <typename URNG>
      result_type operator()(URNG& engine,const param_type& param)
      {
        gamma_dist_type a_param_gamma(param.a()),b_param_gamma(param.b());
        return generate(engine,a_param_gamma,b_param_gamma); 
      }

      result_type min() const { return 0.0; }
      result_type max() const { return 1.0; }

      result_type a() const { return a_gamma.alpha(); }
      result_type b() const { return b_gamma.alpha(); }

      bool operator==(const beta_distribution<result_type>& other) const
      {
        return (param() == other.param() &&
                a_gamma == other.a_gamma &&
                b_gamma == other.b_gamma);
      }

      bool operator!=(const beta_distribution<result_type>& other) const
      {
        return !(*this == other);
      }

    private:
      typedef std::gamma_distribution<result_type> gamma_dist_type;

      gamma_dist_type a_gamma,b_gamma;

      template <typename URNG>
      result_type generate(URNG& engine,gamma_dist_type& x_gamma,gamma_dist_type& y_gamma)
      {
        result_type x = x_gamma(engine);
        return x / (x + y_gamma(engine));
      }
  };

  template <typename CharT,typename RealType>
  std::basic_ostream<CharT>& operator<<(std::basic_ostream<CharT>& os,const beta_distribution<RealType>& beta)
  {
    os << "~Beta(" << beta.a() << "," << beta.b() << ")";
    return os;
  }

  template <typename CharT,typename RealType>
  std::basic_istream<CharT>& operator>>(std::basic_istream<CharT>& is,beta_distribution<RealType>& beta)
  {
    std::string str;
    RealType a,b;
    if (std::getline(is,str,'(') && str == "~Beta" &&
        is >> a && is.get() == ',' && is >> b && is.get() == ')') {
      beta = beta_distribution<RealType>(a,b);
    } else {
      is.setstate(std::ios::failbit);
    }
    return is;
  }

}

像这样使用它:

std::random_device rd;
std::mt19937 gen(rd());
sftrabbit::beta_distribution<> beta(2,2);
for (int i = 0; i < 10000; i++) {
  std::cout << beta(gen) << std::endl;
}

iphone – 在Objective-C中生成一个随机数字长度的随机数

iphone – 在Objective-C中生成一个随机数字长度的随机数

总是当我尝试使用Int = arc4random%MAxnuM生成随机数时,该数字的长度与MAxnuM数字长度-1一样长.
因此,如果我设置arc4random%1000000,则该数字介于100000-999999之间.如何获得78,476或4842等不同的数字?
这是一个样本:

int number = arc4random() % 1000000;
 outputLabel.text = [Nsstring stringWithFormat:@"The Number is %d",number];

谢谢你的帮助,抱歉我的英语不好!

更新:好的,我刚看到,我不会只获得5位数字,但大部分时间.有没有办法通过较少的数字获得更低数字或数字?

解决方法

如果你想获得x到y范围内的随机整数,你可以通过int randomNumber =(arc4random()%y)x来做到这一点.

Python – 具有有限十进制数字的随机浮点数

Python – 具有有限十进制数字的随机浮点数

我刚刚发现了如何在 Python中创建随机数,但是如果我将它们打印出来,它们都有15个十进制数字.我该如何解决这个问题?
这是我的代码:
import random
import os

greaterThan = float(input("Your number will be greater than: "))
lessthan = float(input("Your number will be less than: "))
digits = int(input("Your number will that many decimal digits: "))

os.system('cls')

if digits == 15:
    print(random.uniform(greaterThan,lessthan))

if digits == 14:
    print(random.uniform(greaterThan,lessthan))

if digits == 13:
    print(random.uniform(greaterThan,lessthan))

if digits == 12:
    print(random.uniform(greaterThan,lessthan))

if digits == 11:
    print(random.uniform(greaterThan,lessthan))

if digits == 10:
    print(random.uniform(greaterThan,lessthan))

*(这只是继续下降到0)

我知道你可以像print(“%.2”%someVariable)那样做,但Python用这种方法创建的随机数不保存在任何变量中.至少我是这么认为的.
我还想知道是否有一种方法让变量选择小数点的数量,比如print(“%.”,数字%anotherVariable),但我尝试了,当然它失败了.

我真的不知道.希望你能提供帮助.
谢谢.

解决方法

如果你想从random.uniform获取任何数字并将其截断为特定的位数,你可以使用 round() function.

它允许您舍入到特定的精度.例如:

import random

greaterThan = float(input("Your number will be greater than: "))
lessthan = float(input("Your number will be less than: "))
digits = int(input("Your number will that many decimal digits: "))

rounded_number = round(random.uniform(greaterThan,lessthan),digits)
print(rounded_number)

Python 中来自 PERT 分布的随机值

Python 中来自 PERT 分布的随机值

如何解决Python 中来自 PERT 分布的随机值?

我想在 Python 中从具有以下参数的 PERT 分布生成 10,000 个随机值:low=6898.5、peak=7338.93、high=7705.87

我该怎么做?

解决方法

使用来自 PyPi 的 pertdist,Python 3.9 Win10 x64

代码

from pert import PERT
import seaborn as sns

low=6898.5
peak=7338.93
high=7705.87

pert = PERT(low,peak,high)
sns.kdeplot(pert.rvs(100000))

下图制作

enter image description here

,

如果你只想使用标准库,你可以这样做:

''Get file path and put it in the proper cell
Sub GetFilePath()

    Dim DialogBox As FileDialog
    Dim path As String

    Set DialogBox = Application.FileDialog(msoFileDialogFilePicker)

    DialogBox.Title = "Select quarterly report for " & Range("A" & ActiveCell.Row) & _
        " " & Range("B" & ActiveCell.Row)
    DialogBox.Filters.Clear
    DialogBox.Show

    If DialogBox.SelectedItems.Count = 1 Then
        path = DialogBox.SelectedItems(1)
    End If

    Range("D" & ActiveCell.Row) = path
    Range("D").Column.AutoFit

End Sub

使用 Numpy 大致相同:

from random import betavariate

def pert(a,b,c,*,lamb=4):
    r = c - a
    alpha = 1 + lamb * (b - a) / r
    beta = 1 + lamb * (c - b) / r
    return a + betavariate(alpha,beta) * r

arr = [pert(6898.5,7338.93,7705.87) for _ in range(10_000)]

但大约快 20 倍(20 毫秒对 0.8 毫秒)。

其中任何一个都可用于生成与 Severin 类似的绘图,例如:

KDE via Seaborn

关于Python-生成具有给定数字分布的随机数的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c – 具有Beta分布的随机数发生器、iphone – 在Objective-C中生成一个随机数字长度的随机数、Python – 具有有限十进制数字的随机浮点数、Python 中来自 PERT 分布的随机值等相关知识的信息别忘了在本站进行查找喔。

本文标签: