0

So i have created a code that allows the user to input the size of an array.Then every value is set to 0. The program then takes this code and then adds random 1s to the array. It then outputs the array, printing '.' in the place of a 0 and '*' in place of a 1,but it doesnt randomise the position of the 1s and 0s each time I print it out. I just keep getting the same layout of 1s and 0s. Have i missed out a line of code, or just need to rearrange?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

solid()
{
    int width, height;
    int* myEnvironment(int width, int height, int numWoodchips);
    printf("\nEnter rows and columns : "); 
     scanf("%d %d", &width, &height);

    int myArray[width][height];

    void printEnvironment(int* environment, int width, int height); 
        int i,j;
        printf("\n>Creating grid of size [%d][%d]\n", width, height);

      for (i = 0; i < width; i++)
        {
            for (j = 0; j < height; j++)
             {
                myArray[i][j] = rand() %2;
                if(myArray[i][j]==0)
                     printf("."); 
                else if(myArray[i][j]==1)
                     printf("*");     
             }
        } 

            printf("\n");

        printf("\n>grid of size [%d][%d] created \n", width, height);
        return (myArray[i][j]);

Do i need to return something different?

  • The code you are showing isn't even complete for the function you are showing. Are you initializing random number generator ? – luk32 Apr 25 '14 at 10:10

1 Answers1

2

Couldn't find a good dupe-candidate for this in C.

Add:

srand(time(NULL));

before you start using rand(). This "seeds" the pseudorandom number generator with a hard-to-predict (for this use case) value, and causes it to return a different sequence of numbers.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    How do you know it is the problem when even the function presented isn't complete? Wild guess? – luk32 Apr 25 '14 at 10:11
  • @luk32 Not so wild I think, the code shows use of `rand()` and the description matches this "problem". Many beginner programmers don't understand that `rand()` is just a PRNG, and needs seeding. – unwind Apr 25 '14 at 10:12
  • 2
    [Oh is this a dup ?](http://stackoverflow.com/a/822368) – HamZa Apr 25 '14 at 10:12
  • @unwind Sorry, I meant no personal offence, I just don't like people answering with guesses on poor questions without making them improve the question itself. With no context, no-one can say if he initialized generator or not, and OP should be made aware of it. Maybe I am personally hurt because I wrote a comment, instead of answer when I was only 90% that this is the issue. I feel like I went overboard personally by a little. However, know noone will ever know if this was the problem or not. – luk32 Apr 25 '14 at 11:39
  • @luk32 No worries! I'm not *that* easily offended. :) I understand your motivation, I'm proabably guilty of not spending enough effort to improve questions. Sometimes it feels pointless, so many people who ask don't seem to care about that, they just want a quick answer. :/ – unwind Apr 25 '14 at 11:47