为什么我的 C# 应用程序中的随机像素颜色不那么随机?

发布于 2024-10-07 15:36:55 字数 1668 浏览 0 评论 0原文

我设置了一个代码,随机覆盖位图 2 种不同的颜色,十分之七的颜色为蓝色,十分之三的颜色为绿色。然而,当它完成后,它看起来非常不随机,就像它决定放置几次 7 个蓝色像素,然后放置几次 3 个绿色像素,依此类推。
示例:
替代文本 我的代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FourEx
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(canvas.Image);
            System.Drawing.Imaging.BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, 800, 600), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            unsafe
            {
                int tempy = 0;
                while (tempy < 600)
                {
                    byte* row = (byte*)bmpdata.Scan0 + (tempy * bmpdata.Stride);
                    for (int x = 0; x <= 800; x++)
                    {
                        Random rand = new Random();
                        if (rand.Next(1,10) <= 7)
                        {
                            row[x * 4] = 255;
                        }
                        else
                        {
                            row[(x * 4) + 1] = 255;
                        }
                    }
                    tempy++;
                }
            }
            bmp.UnlockBits(bmpdata);
            canvas.Image = bmp;
        }
    }
}

如果您需要其他信息,请告诉我。

I set up a code to randomly cover a bitmap 2 different colors, 7 out of 10 times the color would be blue, and 3 out of 10 times, the color would be green. However, when it's done it looks very un-random, like it decided to put 7 blue pixels a few times, then 3 green pixels a few times and so on.
Example:
alt text
My code is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FourEx
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(canvas.Image);
            System.Drawing.Imaging.BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, 800, 600), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            unsafe
            {
                int tempy = 0;
                while (tempy < 600)
                {
                    byte* row = (byte*)bmpdata.Scan0 + (tempy * bmpdata.Stride);
                    for (int x = 0; x <= 800; x++)
                    {
                        Random rand = new Random();
                        if (rand.Next(1,10) <= 7)
                        {
                            row[x * 4] = 255;
                        }
                        else
                        {
                            row[(x * 4) + 1] = 255;
                        }
                    }
                    tempy++;
                }
            }
            bmp.UnlockBits(bmpdata);
            canvas.Image = bmp;
        }
    }
}

If you need an additional information, let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

横笛休吹塞上声 2024-10-14 15:36:55

将此行:

Random rand = new Random(); 

移至最外层范围。如果您快速创建这些,许多人将获得相同的时间种子(由于时钟的精度),并将生成相同的“随机”序列。另外,您实际上只需要一个 Random 实例......

private void Form1_Load(object sender, EventArgs e) 
{ 
    Bitmap bmp = new Bitmap(canvas.Image); 
    System.Drawing.Imaging.BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, 800, 600), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

    Random rand = new Random(); 

    unsafe 
    { 
    // ....

move this line:

Random rand = new Random(); 

to the outermost scope. If you create these quickly many will get the same time seed (due to precision of the clock) and will generate the same 'random' sequence. Plus, you only really need one Random instance...

private void Form1_Load(object sender, EventArgs e) 
{ 
    Bitmap bmp = new Bitmap(canvas.Image); 
    System.Drawing.Imaging.BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, 800, 600), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

    Random rand = new Random(); 

    unsafe 
    { 
    // ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文