Reputation: 1313
I want to write a function in C that takes a integer n
as argument and generates n^2
random numbers ( 1 < n <10). I want to store these n^2
numbers in a float *arr
.
arr
is later on passed to another function that rearranges the array into a n*n
matrix.
For example, the first n
elements in the first row, next 'n' in the second row and so on. 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
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
. So how do I do it?
Upvotes: 0
Views: 1274
Reputation: 5619
Use rand()
function for random number generation.
int rand (void);
You need 1
to 10
random number so try this,
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()
.
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.
Upvotes: 0
Reputation: 6088
You can use rand(void)
(located in stdlib.h
). Just remember to initialize a seed with srand()
e.g 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 */
}
}
Upvotes: 0
Reputation: 47933
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
. 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. [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:
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
is a pointer, but we're treating it as if it were an array. 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.
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.
Upvotes: 3