c - Create actual random data in arrays -


i have been working on assignment, have create given number of arrays , fill them random data. approach follow want arrays filled data, percentage. problem every array, random values in same position , not spread how like.

i have been creating arrays in way:

int **array = malloc(doc * sizeof *array); (i = 0; < doc; i++) {     array[i] = malloc(maxwords * sizeof **array); } 

and filling them using :

srand((unsigned) time(&t)); 

and

for(i = 0; < doc; i++){     for(j = 0; j < maxwords; j++){         array[i][rand() %percentage]=rand() %value;     } } 

where

int percentage = rand() %maxwords/10; 

maxwords defines lenght of array doc number of arrays value max random value

as can see random values behaving identically. know has way srand depends on time generate numbers, , execution of program fast, similar data because of "similar" time. asking either different day generate random values or trick fill arrays differently.

with "rand() % percentage" picking elements within first 10% of each array. instead, want this:

for (i = 0; < doc; ++i){     (j = 0; j < maxwords; ++j) {         if (rand() % 100 <= 10) {             array[i][j] = rand() % value;         }     } } 

this gives each elements in array 10% chance of being initialized, should result (for large enough arrays) in 10% of elements being initialized.

if want 10% of array initialized, instead placing indices (0...j) array, randomizing array, , picking first maxwords/10 indices randomized array initialization.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -