Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:45:14+00:00 2026-06-17T10:45:14+00:00

Possible Duplicate: Random number generator only generating one random number Random not that random

  • 0

Possible Duplicate:
Random number generator only generating one random number
Random not that random

This is my first attempt at using classes and objects and I have absolutely no clue as to how to handle the situation I’m currently in. I’m creating a game which requires the spawning of enemies at the edges of the screen, so I created a method that creates 10 objects and randomly selects an edge and a suitable coordinate to assign to them.

    private void spawner()
    {
        if (enemies.Count < 10)
        {
            int a;
            a = 10 - enemies.Count;

            for (int i = 0; i < a; i++)
            {
                int x, y, c;
                Random random = new Random();
                c = random.Next(0, 4);

                if (c == 0)
                {
                    x = random.Next(0, 839);
                    y = 1;
                }
                else if (c == 1)
                {
                    y = random.Next(0, 647);
                    x = 1;
                }
                else if (c == 2)
                {
                    x = 839;
                    y = random.Next(0, 647);
                }
                else
                {
                    y = 647;
                    x = random.Next(0, 839);
                }

                enemies.Add(new Enemy(5, 25, 3, new Point(x, y)));
                enemies[enemies.Count - 1].adjustPosition(player1.centreX, player1.centreY);
            }
        }
    }

I then call the method within the Enemy class to make the coordinates shift closer to the centre of the Player controlled object as well as create 3 new points that will be used to draw the enemy: enemies[enemies.Count – 1].adjustPosition(player1.centreX, player1.centreY);

    public class Enemy
    {
        public int enemyLife { get; set; }
        public Point enemyCentre { get; set; }
        public int enemyRadius { get; set; }
        public double enemySpeed { get; set; }
        public Point[] enemyVertices { get; private set; }

        public Enemy(int life, int radius, double speed, Point centre)
        {
            enemyLife = life;
            enemyCentre = centre;
            enemyRadius = radius;
            enemySpeed = speed;
        }

        public void adjustPosition(int centreX, int centreY)
        {
            double vx = 0;
            double vy = 0;
            double mag = 0;
            int x, y;

            if (enemyCentre.X != 0)
            {
                vx = centreX - enemyCentre.X;
                vy = centreY - enemyCentre.Y;
                mag = Math.Sqrt(vx * vx + vy * vy); // length

                vx /= mag;
                vy /= mag;

                x = (int)((double)enemyCentre.X + vx * enemySpeed);
                y = (int)((double)enemyCentre.Y + vy * enemySpeed);
                enemyCentre = new Point(x, y);

                int px, py, px2, py2, px3, py3;
                double angle;

                vx = centreX - x;
                vy = centreY - y;
                mag = Math.Sqrt(vx * vx + vy * vy); // length

                vx /= mag;
                vy /= mag;

                px = (int)((double)x + vx * 20);
                py = (int)((double)y + vy * 20);

                angle = Math.Atan2(centreY - y, centreX - x);

                px2 = (int)(px + (-30 * Math.Cos(0.785398163 + angle)));
                py2 = (int)(py + (-30 * Math.Sin(0.785398163 + angle)));

                px3 = (int)(px + (-30 * Math.Cos(-0.785398163 + angle)));
                py3 = (int)(py + (-30 * Math.Sin(-0.785398163 + angle)));

                Point[] points = new Point[3];
                points[0] = new Point(px, py);
                points[1] = new Point(px2, py2);
                points[2] = new Point(px3, py3);

                enemyVertices = points;
            }
        }
    }

The problem is that when I attempt to execute the program using ‘Start Debugging’ every created object shares the same centre coordinates with the the last added object to the list. Yet when I add a breakpoint before the objects are added to the list and Step through the code, the objects are assigned random values as they should be. Also if I remove the breakpoint after manually executing the code for some of the objects, the objects that been run manually will contain random values while the rest of the objects will share the same values. Does anyone know what could be causing this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:45:15+00:00Added an answer on June 17, 2026 at 10:45 am

    That’s because you’re re-creating the Random() object in your loop. This object generates pseudo-random numbers using a time-based seed, so several Random() objects created in a very fast loop will generate the exact same values in the same order.

    When you put a breakpoint in the middle of the loop, you cause a delay changing the seed of the Random() object.

    The simplest solution for this is to create the Random() object only once for this method and reuse it during the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Random number generator only generating one random number My code below is
Possible Duplicate: Random number generator only generating one random number I have a method
Possible Duplicate: Random number generator only generating one random number class System.Random .. why
Possible Duplicate: Why does this Random Number Generator not random? I have this test
Possible Duplicate: Random permutation of integers using a random number generator For example I
Possible Duplicate: Why does it appear that my random number generator isn't random in
Possible Duplicate: Random number generator not working the way I had planned (C#) I
Possible Duplicate: Why does it appear that my random number generator isn't random in
Possible Duplicate: Random number generator not working the way I had planned (C#) I
Possible Duplicate: Random number generator not working the way I had planned (C#) I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.