So I'm trying to make a battleship game, generating random position of ships at beginning. I'm using a array of integers to keep track of where are the ships. I wish to generate a random sequence of numbers with given argument of how much numbers. Say i want 4 numbers, which are all in sequence like 1,2,3,4 but I can also randomly get 55,56,57,58 ? Any ideas? 4 numbers would represent aircraft carrier, 3 destroyer etc etc..
Asked
Active
Viewed 4,301 times
0
-
say the range is 0-100 and you want 4 random numbers in sequence. You could generate 1 random number (call it w) that's between 0-100. Then generate a second number in the range of w-100 (call second number x) and then generate a 3rd in range of x - 100 (call y). Generate a 4th in range z-100 (call z). Now you have w,x,y,z which are random numbers in order. Instead of using variables you could just read them into an array and return the array. – Jacobr365 Mar 09 '16 at 18:17
-
If you're talking about the classic battleship game of putting pieces onto a grid then your going to need more than just a sequence of numbers. You need a position, a direction and then you have to make sure it doesn't cross over already placed ships. – juharr Mar 09 '16 at 18:18
-
You don't really need to generate the whole sequence, just the starting point. You might write a loop which generates a random starting point, randomly chooses a direction, checks if a ship (or board edge) is already intersecting with that vector, and places the ship. – David Mar 09 '16 at 18:18
-
@juhaar Well I was thinking of once I generate say random 4 number sequence (that represents one ship) say...55,56,57,58 I would mark those spots in my array with 1 (whole array is 0 initially) and before assigning the value I would check if there's any 1s in those spots already, so that would get rid of collision? – maran Mar 09 '16 at 18:25
-
@David any suggestions how would I go about writing that loop? I don't have any idea atm really – maran Mar 09 '16 at 18:26
-
@maran: I guess start here?: https://msdn.microsoft.com/en-us/library/f0e10e56.aspx – David Mar 09 '16 at 18:27
-
@maran: You write loops like this: you must figure out three things. (1) what must happen before the loop? This is the precondition of the loop. (2) what is the condition that must be true for the loop to execute at least one more time? This is the loop condition. (3) what must happen during each loop execution? This is the loop body. You then write the loop like this: `precondition-code; while ( loop-condition ) { loop-body } ` What are the three things for your loop? – Eric Lippert Mar 09 '16 at 18:31
2 Answers
1
As others have pointed out, this won't actually help much in Battleship where ships can be placed in different directions, but to answer the question you asked, just generate one random number and then increment.
private static Random _random = new Random();
private static IEnumerable<int> GetSequence(int size, int max)
{
var start = _random.Next(max - (size - 1));
for (var i = 0; i < size; i++)
{
yield return start + i;
}
}
Edit: As @EricLippert mentions, this can be shortened to:
private static Random _random = new Random();
private static IEnumerable<int> GetSequence(int size, int max)
{
return Enumerable.Range(_random.Next(max-(size-1)), size);
}

Ed T
- 410
- 4
- 11
-
You can make your already-short program considerably shorter: a simple `return Enumerable.Range(_random.Next(max-(size-1)), size);` will do. – Eric Lippert Mar 09 '16 at 18:34
0
private static Random rand = new Random();
static void Main(string[] args)
{
int[] numbers = RandomNumbers(4, 1, 100);
Console.WriteLine(string.Join(",",numbers));
Console.Read();
}
public static int[] RandomNumbers(int numberOfResults, int minValue, int maxValue)
{
// declare array for return values
int[] result = new int[numberOfResults];
// get next value from random passing in the min and max ranges
int start = rand.Next(minValue, maxValue);
// generate the numbers up to the max number of results required
for (int i = 0; i < numberOfResults; i++)
{
result[i] = start++;
}
return result;
}

G54hota
- 1
- 1
-
-
Ahh yes you are right @Ed T. I like your solution. Nice, clean and short. – G54hota Mar 09 '16 at 20:29