试图在数组中生成一定数量的随机数

时间:2022-06-29 21:28:50

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...

使用这个程序,我尝试将一个6x15的数组设置为0,然后将一个随机数x的数量放入数组的一个随机槽中。然而,事情并没有按计划进行……

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

注意MAX_ROWS是6,MAX_COLS是15,OCEAN是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):

这应该放置随机数。(shipnum只是我用来限制我放置的船只数量的一个值):

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;
}

4 个解决方案

#1


2  

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.

您的代码还允许在已有ship的地方编写ship。如果您不想允许这样做,您应该在将ship放入该单元格之前检查单元格是否为空。

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;
}

#2


3  

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()

每次调用rand_number时,都要重置带有srand((unsigned int)时间(NULL)的pseudo rng;,删除该行,或将其移动到main()的开头

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

不要在程序中多次调用srand(),在程序启动时不要调用。

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.

取决于您想要做什么,请记住,随机数生成器不会生成唯一的数字,因此如果您希望保证在数组中放置了shipnum条目,您可能需要对此进行说明。例如,您对rand_number()的6个调用都可以生成4个。

#3


0  

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.

除了不多次调用srand()之外,您不会使用while循环获得所需的输出。你说你想要总共有6艘船,1 1 1 2 2 3,但是你的while循环放置了每种类型的一艘,之后你的shipnum = 0,把你从循环中打断。

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.

相反,创建三个单独的ship放置例程,每个类型一个。此外,您需要确保您的位置不是位于另一艘船的顶部——这正是在您的随机函数中快速调用srand()时发生的情况,并且在其他时间也可能发生。

#4


0  

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.

首先让我感到吃惊的是,你没有检查现有的单元格是否已经有了一个指定的船只。我用0作为前哨值。

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.

使种子外部(在函数体外部声明),并初始化它。

#1


2  

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.

您的代码还允许在已有ship的地方编写ship。如果您不想允许这样做,您应该在将ship放入该单元格之前检查单元格是否为空。

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;
}

#2


3  

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()

每次调用rand_number时,都要重置带有srand((unsigned int)时间(NULL)的pseudo rng;,删除该行,或将其移动到main()的开头

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

不要在程序中多次调用srand(),在程序启动时不要调用。

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.

取决于您想要做什么,请记住,随机数生成器不会生成唯一的数字,因此如果您希望保证在数组中放置了shipnum条目,您可能需要对此进行说明。例如,您对rand_number()的6个调用都可以生成4个。

#3


0  

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.

除了不多次调用srand()之外,您不会使用while循环获得所需的输出。你说你想要总共有6艘船,1 1 1 2 2 3,但是你的while循环放置了每种类型的一艘,之后你的shipnum = 0,把你从循环中打断。

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.

相反,创建三个单独的ship放置例程,每个类型一个。此外,您需要确保您的位置不是位于另一艘船的顶部——这正是在您的随机函数中快速调用srand()时发生的情况,并且在其他时间也可能发生。

#4


0  

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.

首先让我感到吃惊的是,你没有检查现有的单元格是否已经有了一个指定的船只。我用0作为前哨值。

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.

使种子外部(在函数体外部声明),并初始化它。