简体   繁体   English

在C中生成随机数的函数

[英]Function that generates a random number in C

I want to write a function in C that takes a integer n as argument and generates n^2 random numbers ( 1 < n <10). 我想用C编写一个函数,该函数将整数n作为参数并生成n^2随机数(1 <n <10)。 I want to store these n^2 numbers in a float *arr . 我想将这些n^2数字存储在float *arr
arr is later on passed to another function that rearranges the array into a n*n matrix. 稍后将arr传递给另一个函数,该函数将数组重新排列为n*n矩阵。 For example, the first n elements in the first row, next 'n' in the second row and so on. 例如,第一行中的前n元素,第二行中的下一个'n',依此类推。 How can I generate the random numbers so that all rows in the end matrix are not the same. 如何生成随机数,以使末尾矩阵中的所有行都不相同。

When n was small like 3 I was creating the array manually like 当n小如3时,我手动创建数组,如

float* arr;
float temp[9] = {1,1,1,1,1,1,1,1,1};
arr = temp;

I want to test it for bigger values of n . 我想测试它的较大值n So how do I do it? 那我该怎么办呢?

Well, first of all, you need to understand that when you wrote 好吧,首先,您需要了解当您写

arr = malloc(9*sizeof(float));
float temp[9] = {1,1,1,1,1,1,1,1,1};
arr = temp;

you wasted the call to malloc . 您浪费了对malloc的调用。 When you said 当你说

arr = temp;

you weren't copying the array contents, you were just rearranging pointers. 您没有复制数组内容,只是重新排列了指针。 At first arr had pointed to the region you allocated with malloc , but then later it pointed to the array temp , and the pointer to the malloc 'ed memory was lost. 最初, arr指向使用malloc分配的区域,但后来指向数组temp ,指向malloc的内存的指针丢失了。 [But you've fixed that problem now, so we can move on.] [但是您现在已经解决了这个问题,因此我们可以继续进行。]

If you want to fill in the 9 values without using a fixed array like temp , that's easy: 如果要填写9个值而不使用temp这样的固定数组,那很简单:

int n = 3;
int arrsize = n * n;
int i;
float *arr = malloc(arrsize * sizeof(float));
for(i = 0; i < arrsize; i++) arr[i] = 1.0;

Here, the fact that we can write arr[i] = 1.0 is hugely important. 在这里,我们可以写arr[i] = 1.0的事实非常重要。 arr is a pointer, but we're treating it as if it were an array. arr是一个指针,但我们将其视为数组。 This is the famous "equivalence of arrays and pointers" in action, and if you're not comfortable with it yet, now's the time to learn. 这就是著名的“数组和指针的等效性”,如果您还不熟悉它,那么现在该学习一下。

You also said you wanted random numbers, so I assume you don't really want to set every element of the array to 1. Here's a quick way to get random numbers -- I'm choosing them from 1 to 10, with a resolution of 0.01. 您还说过您想要随机数,所以我假设您并不是真的希望将数组的每个元素都设置为1。这是获取随机数的快速方法-我从1到10的范围内选择了一个分辨率为0.01。

for(i = 0; i < arrsize; i++) arr[i] = 1. + rand() % 900 / 100.;

There are some problems with this which we can talk about, but it's a decent start. 我们可以谈论一些问题,但这是一个不错的开始。

Use rand() function for random number generation. 使用rand()函数生成随机数。

int rand (void);

You need 1 to 10 random number so try this, 您需要110随机数,因此请尝试此操作,

var = rand() % 10 + 1;     // var in the range 1 to 10

You can follow the procedure: 您可以按照以下步骤进行:

n = input
float* arr = malloc arr to n^2 
//initialize random seed
srand (time(NULL));
begin for loop (i = 0 to n^2-1)
    arr[i] = rand() % 10 + 1;
end for

Note that, the pseudo-random number generator is initialized using the argument passed as seed into srand() . 请注意,伪随机数生成器使用作为种子传递给srand()的参数进行初始化。

For every different seed value used in a call to srand , the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand. 对于调用srand使用的每个不同种子值,可以期望伪随机数生成器在随后的rand调用中生成不同的结果序列。

You can use rand(void) (located in stdlib.h ). 您可以使用rand(void) (位于stdlib.h )。 Just remember to initialize a seed with srand() eg srand(time(NULL)) . 只需记住使用srand()初始化种子即可,例如srand(time(NULL))

void fillRow(float* row, size_t count) {
    size_t i;
    for(i = 0; i < count; ++i) {
        row[i] = (rand() % 10) + 1; /* this will generate a random number between 1 and 10 */
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM