Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:59:44+00:00 2026-05-27T23:59:44+00:00

I’m taking a C refresher and took on a board game as an exercise.

  • 0

I’m taking a C refresher and took on a board game as an exercise. The board game is “Game of the Generals” and is pretty much like chess as it uses pieces with ranks on an 8×8 square.

Basically the implementation of a board is a two-dimensional array of a particular struct. So a square of the board can be accessed through its indices, much like an x-y coordinate system.

Now I decided to randomly distribute the pieces across the board, and the logic is to generate a random x-y coordinate, check if a piece already resides on those coordinates on the board, and populate it with a piece if it is available. If it is not, then another random x-y coordinate is generated. This goes on until all pieces are accounted for.

I’m using rand() to generate random numbers within a specific range (I’m using a modulo operator and a padding number to dictate the range. See code below)

But rand() doesn’t seem to give random enough numbers for me to work with. I keep ending up with the same piece distribution over and over again! (But interesting enough, I can generate a different distribution on a Mac, but the distribution is still consistent!)

See code below as to how I’m using rand() to generate a number with a range.

void initPieces(){

    int player, rank_index, population, rand_min, rand_x, rand_y;

    for(player = 1; player <= 2; player++){

       if(player == 1){
            rand_min = 5;
        }else{
           rand_min = 1;
        }

        for(rank_index = 0; ir < sizeof ranking/sizeof ranking[0]; rank_index++){

            for(population = 0; population < getRank(rank_index)->population; population++){

               do{
                   rand_x = (rand() % 8) + 1;
                   rand_y = (rand() % 4) + rand_min;

               }while((getGrid(rand_x,rand_y))->has_piece == 1);

               assignPiecetoGrid(player,rank_index,rand_x,rand_y);

            }

       }

   }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:59:45+00:00Added an answer on May 27, 2026 at 11:59 pm

    You need to realize that rand() is a pseudorandom number generator, and it is specifically engineered to return the same sequence of numbers for a given seed. The seed is set with the srand() function.

    srand(0);
    printf("the first rand() with seed 0 is %d\n", rand());
    srand(1);
    printf("the first rand() with seed 1 is %d\n", rand());
    srand(0);
    printf("the first rand() with seed 0 is still %d\n", rand());
    

    So, the way to make it less predictable is generally to re-seed it from something a bit more random, or at least from something that is not the same every time you run the program:

    srand(time(NULL));

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.