python - Python的随机性的随机性

标签 python random

我正在使用 Python 生成图像,并使用虚线进行点画。划线的周期是恒定的,改变的是划线/空间比。这会产生这样的结果:

enter image description here

但是,在该图像中,虚线具有统一的原点,这会产生难看的垂直排水沟。所以我试图随机化原点以去除排水沟。这种工作,但有一个明显的模式:

enter image description here

想知道这是从哪里来的,我做了一个非常简单的测试用例,上面有堆叠的虚直线:

  • 冲刺率:50%
  • 短划线周期 20 像素
  • 使用 random.uniform(-10.,+10.)(*) 将原点从 -10px 转移到 +10px(在初始 random.seed() 之后)

enter image description here

并增加随机性:

enter image description here

所以还是有规律的。我不明白的是,要获得可见的排水沟,您需要有 6 或 7 个连续值落在同一范围内(例如,总范围的一半),这应该是 1/64 的概率,但似乎经常发生在生成的 200 行中更常见。

我是不是误会了什么?只是我们人类的大脑在看到没有的模式吗?有没有更好的方法来生成更“视觉随机”的东西(python 2.7,最好不安装任何东西)?

(*) 部分像素在该上下文中有效

附件:我使用的代码(这是一个 Gimp 脚本):

#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

# Python script for Gimp (requires Gimp 2.10)
# Run on a 400x400 image to see something without having to wait too much
# Menu entry is in "Test" submenu of image menubar

import random,traceback
from gimpfu import *

def constant(minShift,maxShift):
    return 0

def triangle(minShift,maxShift):
    return random.triangular(minShift,maxShift)

def uniform(minShift,maxShift):
    return random.uniform(minShift,maxShift)

def gauss(minShift,maxShift):
    return random.gauss((minShift+maxShift)/2,(maxShift-minShift)/2)

variants=[('Constant',constant),('Triangle',triangle),('Uniform',uniform),('Gauss',gauss)]

def generate(image,name,generator):
    random.seed()
    layer=gimp.Layer(image, name, image.width, image.height, RGB_IMAGE,100, LAYER_MODE_NORMAL)
    image.add_layer(layer,0)
    layer.fill(FILL_WHITE)
    path=pdb.gimp_vectors_new(image,name)

    # Generate path, horizontal lines are 2px apart, 
    # Start on left has a random offset, end is on the right edge right edge
    for i in range(1,image.height, 2):
        shift=generator(-10.,10.)
        points=[shift,i]*3+[image.width,i]*3
        pdb.gimp_vectors_stroke_new_from_points(path,0, len(points),points,False)
    pdb.gimp_image_add_vectors(image, path, 0)

    # Stroke the path
    pdb.gimp_context_set_foreground(gimpcolor.RGB(0, 0, 0, 255))
    pdb.gimp_context_set_stroke_method(STROKE_LINE)
    pdb.gimp_context_set_line_cap_style(0)
    pdb.gimp_context_set_line_join_style(0)
    pdb.gimp_context_set_line_miter_limit(0.)
    pdb.gimp_context_set_line_width(2)
    pdb.gimp_context_set_line_dash_pattern(2,[5,5])
    pdb.gimp_drawable_edit_stroke_item(layer,path)

def randomTest(image):
    image.undo_group_start()
    gimp.context_push()

    try:
        for name,generator in variants:
            generate(image,name,generator)
    except Exception as e:
        print e.args[0]
        pdb.gimp_message(e.args[0])
        traceback.print_exc()

    gimp.context_pop()
    image.undo_group_end()
    return;

### Registration
desc="Python random test"

register(
    "randomize-test",desc,'','','','',desc,"*",
    [(PF_IMAGE, "image", "Input image", None),],[],
    randomTest,menu="<Image>/Test",
)

main()

最佳答案

可以这样想:排水沟是可察觉的,直到它被阻塞(或几乎如此)。只有当两条连续的线几乎完全异相时才会发生这种情况(第一条线上的黑色线段几乎位于下一条线的白色线段之上)。这种极端情况大约每 10 行就会发生一次,因此可见的排水沟在被阻塞之前似乎延伸了大约 10 行。

换个角度看——如果您打印出图像,确实有较长的白色 channel ,您可以通过这些 channel 轻松地用笔画一条线。为什么你的大脑不应该感知它们?

为了获得更好的视觉随机性,找到一种方法使连续的线条依赖而不是独立,这样几乎异相的行为会更频繁地出现。

关于python - Python的随机性的随机性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55934019/

相关文章:

java - 而 else 语句等同于 Java?

android - 在android中随机排列图像

random - 从表中获取一个值并将其分配给另一个值,不重复

python - 使用 spacy 删除停用词

python - 如何在 Python 中使用 Google Drive API 创建新文件夹?

Python+ubuntu错误

Python单元测试-可能会一直失败直到通过吗?

javascript - 尝试生成随机数时遇到的问题

string - 如何生成一个由字母数字字符组成的随机字符串?

c - 独立于时间在 C 中播种 rand()