开发者

Multidimensional array with rand()

开发者 https://www.devze.com 2023-03-04 14:09 出处:网络
I want to create a multidimensional array with just two values : 0 or 1. I use the srand/rand functions but array contains only 0.

I want to create a multidimensional array with just two values : 0 or 1.

I use the srand/rand functions but array contains only 0. Here is the code :

#define NB_LINE 4
#define NB_COLUMN 11
int tab[NB_LINE][NB_COLUMN] ;  // global variable
    void generate() {
    srand((unsigned int)time(NULL));

    int n, i, j;
    for(n = 0; n < NB_LINE*NB_COLUMN; ++n){
        do
        {
            i = rand() % NB_LINE;
            j = rand() % NB_COLUMN;
        }
        while (tab[i][j] != 0);
        tab[i][j] = 1 ;
    }
}

I don't know how to solve this problem ?

thanks !

Edit : Thanks for your answers. Do you think it's possible with rand() to have juste one "1" per column a开发者_StackOverflow中文版nd others spots contain only 0 ?


Your loop doesn't do what you think it does. Try this:

int tab[NB_LINE][NB_COLUMN] ;  // global variable
void generate() {
    srand((unsigned int)time(NULL));

    int i, j;
    for (i=0; i < NB_LINE; ++i) {
        for (j=0; j < NB_COLUMN; j++) {
            tab[i][j] = rand() % 2;
        }
    }  
}

At the end of that, you will have an array where each spot randomly has a 1 or a 0.


I want to create a multidimensional array with just two values : 0 or 1

You should take the % with 2 on a random number generated. With that hint given, try to make the program more simpler and easy to understand. Why don't you just fill each element of the array sequentially ?


Edited question

Do you think it's possible with rand() to have just one "1" per column and others spots contain only 0?

Yes, of course.

Instead of looping over all lines and all columns setting each individual array element to a random value of 0 or 1 do initialize all array elements to 0 (if they aren't already); loop over all columns and choose 1 random line and set the corresponding array element to 1.

#include <stdlib.h>
#include <string.h> // memset
#include <time.h>

void generate(int *arr, size_t lines, size_t columns) {
    int col, row;
    memset(tab, 0, lines * columns * sizeof *arr);
    for (col = 0; col < columns; col++) {
        row = rand() % lines;
        tab[row * columns + col] = 1;
    }
}

int main(void) {
    int tab[NB_LINE][NB_COLUMN];
    srand(time(0));                           /* no casts */
    generate(&tab[0][0], NB_LINE, NB_COLUMN);
    /* ... */
}

Notice I've removed the srand() from the generate() function. srand() should be called no more than once per program invocation (to guarantee maximum randomness). The best way to accomplish that is to call srand from main().
Also, instead of i and j, I named the variables row and col.
And, I've made tab a local variable rather than global. Avoid globals whenever possible: it's a favor you do yourself. With no globals the information is passed to the function through arguments.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号