Question

Using this program, I'm trying to set a 6x15 array to 0, then place a random number x amount of times in a random slot of the array. However, it doesn't go as planned...

Note that MAX_ROWS is 6, MAX_COLS is 15, and OCEAN is 0

#include <stdio.h>
#include <time.h>
#include "util.h"

int rand_number(int param);

main()
{
int map[MAX_ROWS][MAX_COLS]; //initializes an array, map, with the dimensions 6 and 15.

//sets all values in the array to 0
int a,b;

for (a = 0; a < MAX_ROWS; a++)
{
for (b = 0; b < MAX_COLS; b++)
{map[a][b]=OCEAN;}
}

int shipnum = 6;

This SHOULD place the random numbers. (shipnum is just a value I use to limit the number of ships I place):

while(shipnum > 0)
{
map[rand_number(MAX_ROWS)][rand_number(MAX_COLS)] = 3;
shipnum -= 1;
map[rand_number(MAX_ROWS)][rand_number(MAX_COLS)] = 2;
shipnum -= 2;
map[rand_number(MAX_ROWS)][rand_number(MAX_COLS)] = 1;
shipnum -= 3;
}

However, when I run

/*This will print the array*/
for (a = 0; a < MAX_ROWS; a++)
{
for (b = 0; b < MAX_COLS; b++)
{printf("%d ", map[a][b]);}
printf("\n");
}
}

I am given

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

When I actually want to get something like

0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 2 0 0 0 0 0 3 0 0 
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 

This is the function I use to generate a random number.

/*This will generate a random number*/
int rand_number(int param)
{
srand((unsigned int)time(NULL));
int x = param;
int rn = rand() % x;
return rn;
}
Was it helpful?

Solution

You are reseeding your random generator on every call which will make it not random. Remove this line:

srand((unsigned int)time(NULL));

Your code also allows writing a ship where there already was a ship. If you don't want to allow this, you should check that the cell is empty before placing a ship in that cell.

int x = rand_number(MAX_ROWS);
int y = rand_number(MAX_COLS);
if (map[x][y] == 0) {    /* Add this check! */
    map[x][y] = 1;
    shipnum -= 1;
}

OTHER TIPS

Every time you call rand_number, you reset the pseudo rng with srand((unsigned int)time(NULL)); , remove that line, or move it to the start of your main()

Don't call srand() more than once in a program, somwhere at program startup.

Depending on what you want to do, remember a random number generator doesn't produce unique numbers, so you might need to account for that if you want to guarantee you've placed shipnum entries in your array. I.e. your 6 calls to rand_number() could all produce 4.

In addition to not calling srand() multiple times, you're not going to get your desired output with the while loop you have. You're stating you want a total of 6 ships, 1, 1, 1, 2, 2, 3, however your while loop places one of each type, after which your shipnum will be == 0, breaking you out of the loop.

Instead, create three separate ship placement routines, one for each type. Additionally, you need to make sure your placement is not on top of another ship -- which is exactly what's happening when you srand() within your random function when called so rapidly, and is possible to happen at other times as well.

First thing that strikes me is that you don't check if the existing cell already has a ship assigned or not. I would use 0 as a sentinel value.

Also (it's been some time since I worked with random numbers), I'm betting there's something wrong with your seed. I believe the computer is too fast for your function to succeed, meaning that the 'time' you use as the seed is always the same.

Make the seed external (declare it outside the function body), and initialize it once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top