how to create a world with 0s and 1s arranged randomly.

–1 vote
asked Sep 7, 2019 by anonymous

1 Answer

0 votes
answered Oct 28, 2019 by Redstoneboi (540 points)

What kind of world?

If it's just a line of 1's and 0's, then:

#include <stdlib.h>

// create a random "world" with custom length
int* randomWorld(int length) {
    // create an array "world" containing {length} integers
    int *world = malloc(length * sizeof(int));

    // set each integer in "world" to a random number from 0 to (2 - 1)
    // meaning make every element 0 or 1
    for (int i = 0; i < length; i++) world[i] = rand() % 2;

    // get The World back
    return world;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...