0

I am working on a 2d project and one of the things I need to implement is : 3 sprites (seahorses) and during creation I should assign a random x and random y coordinate based on the viewable screen (-400;0;400). They should also be assigned a random speed between 5 and 10 . The seahorses should move up for 100px and down 100px from the center point. So my question is : How?! I tried using the random - Random position = new Random(); but nothing really happens when I try to use it.

2
  • 1
    Welcome to Stack Overflow! It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask May 2, 2014 at 18:08
  • Please do not deface your questions. I have rolled back your destructive edit.
    – talonmies
    May 3, 2014 at 13:54

2 Answers 2

1
Random rnd = new Random()
rnd.Next(5, 10+1) // Exclusive upper bound

To generate a new number, just reuse rnd.Next(min, max+1)

MSDN : http://msdn.microsoft.com/en-us/library/system.random.aspx

1

I'm hoping some example code will set you off in the right direction. This is code written in a unit test project. Since you've given no example code, this is the best I can do for you.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyNamespace
{
    [TestClass]
    public class OtherTests
    {
        readonly Random rand = new Random();

        [TestMethod]
        public void CoordTest()
        {
            double width = 400;
            double height = 400;
            double centerScreenX = width / 2;
            double centerScreenY = height / 2;

            double minSpeed = 5;
            double maxSpeed = 10;

            double offsetRangeX = 0;
            double offsetRangeY = 100; // Only up and down.


            List<double[]> vectors = new List<double[]>();

            int spritesToGenerate = 3;
            while (spritesToGenerate-- > 0)
            {
                vectors.Add(new[]
                {
                    centerScreenX + rand.NextDouble() * offsetRangeX * 2 - offsetRangeX,
                    centerScreenY + rand.NextDouble() * offsetRangeY * 2 - offsetRangeY,
                    rand.NextDouble() * (maxSpeed - minSpeed) + minSpeed,
                    rand.NextDouble() * (maxSpeed - minSpeed) + minSpeed
                });
            }
            Console.WriteLine(string.Join("\r\n", vectors.Select(v => string.Join("\t", v))));
        }
    }
}
2
  • 2
    You should use a global Random otherwise the random.next can return the same values if the method is called very fast. May 2, 2014 at 17:53
  • I agree and I've edited the example.
    – Chris
    May 2, 2014 at 17:54