Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: Rendering a canvas at Random Points
    primarykey
    data
    text
    <p><em>With reference to <a href="https://stackoverflow.com/questions/505040/developing-a-robocode-type-game-with-net-for-a-school-assignment">this programming game</a> I am currently building.</em></p> <p>When the game is started, I am generating these robots at supposingly random points on a Canvas, and at first look (adding one or two bots at the same time), this seemed to be working as it should.</p> <p>...but, when I added a ton of bots at the same time, this is how they where "randomly" appeared on the canvas:</p> <p><a href="http://img22.imageshack.us/img22/6895/randombotpositionlf7.jpg" rel="nofollow noreferrer">alt text http://img22.imageshack.us/img22/6895/randombotpositionlf7.jpg</a></p> <p>The supposedly random points don't seem that random after all...they're just <strong>points of a straight line</strong>!</p> <hr> <p>Here is how I am calculating the Points:</p> <pre><code>SetStartingPoint(GetRandomPoint(ArenaWidth, ArenaHeight)); //the width and height are 550, 480 at the moment //which calls: private Point GetRandomPoint(double maxWidth, double maxHeight) { return new Point(new Random().Next(0, (int)(maxWidth-80)), new Random().Next(0, (int)maxHeight)); } //and ultimately: private void SetStartingPoint(Point p) { Translate_Body.X = (double)p.X; Translate_Body.Y = (double)p.Y; } </code></pre> <p>As regards the above code, <code>Translate_Body</code> is of type <code>TranslateTransform</code> of the robot (<code>canvas</code>), so by assigning its <code>X</code> and <code>Y</code> properties, it will change its position to the new values</p> <hr> <p>What am I missing here?</p> <hr> <p><strong>[UPDATE] Solution:</strong></p> <p>The problem was like you all suggested, the numbers weren't being seeded properly because of the new instantiations.</p> <p>I now changed the code to use a single <code>Random</code> variable and to seed all the points from it.</p> <p><strong>But I still can't understand why the points where being generated at a seemingly straight line of coordinates. <em>Is anyone able to explain this ?</em></strong> </p> <hr>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload