C#C
C#2y ago
kcm21

Not so random, random drawing...

I'm doing some drawing in C# as a fun way to learn more about the language, and I'm getting some unexpected results from the Random() method.

When trying to distribute a star throughout a black background by entering a random x & y coordinate, the icons just follow somewhat of a line from top left to bottom right. They're placed in a different pattern each time, but still along the same line (see screenshots).

Thanks for any suggestions you might give!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;

namespace Drawing1
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}

private void FormMain_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Icon starry = new Icon(@"C:\Users\kodym\source\repos\Drawing1\Star.ico");
var rand1 = new Random();
var rand2 = new Random();
for (int i = 0; i < 20; i++)
{
g.DrawIcon(starry, rand1.Next(1000), rand2.Next(1000));
}

}
}
}
Screenshot_2024-03-04_131631.png
Screenshot_2024-03-04_131651.png
Was this page helpful?