2

I am creating a 5x5 board game in C. The problem I am having is that when I use the rand() it duplicates some of the numbers. How do I stop the duplicates and 0's?

Sorry if too basic a question. I'm new to C.

int createboard() {

  int rows;
  int columns;
  int board[5][5];
  int board2[5][5];

  srand(time(NULL));

  for (rows = 0; rows < 5; rows++) {
    for (columns = 0; columns < 5; columns++) {
      randomNumber = rand() % 25;
      board[rows][columns] = randomNumber;
    }
  }
}
5
  • 4
    My question is why you thought to use rand(). You clearly don't want random numbers; you want a random order of a specific set of numbers, which is a very different thing, so generating random numbers isn't the answer. Commented Jan 31, 2018 at 15:25
  • See stackoverflow.com/q/3343797/2410359 Commented Jan 31, 2018 at 15:31
  • You have to keep a track of numbers that are already generated. Run a while loop around the rand function, and bail out of it when the new unique number is taken. That is a simplest way. This could be helped with an additional array containing 25 Boolean values corresponding to the status of the random number (already generated = true, new number = false). I have to post it here so @Quicky can see it. Otherwise it is down-voted. Commented Jan 31, 2018 at 18:36
  • rand is a standard C function and it may be used for generating random numbers. More about the quality of this function is here: cboard.cprogramming.com/c-programming/… Commented Jan 31, 2018 at 18:46
  • @VladP: You ought to put answers in the answers section, where they can be peer-reviewed which perhaps includes a downvote. Don't take downvotes personally, I don't, and at the time of my writing, my answer below has a downvote. Commented Feb 1, 2018 at 8:33

3 Answers 3

8

rand() would not be a particularly good generator if the probability of drawing the same number twice was zero.

A standard approach here would be to generate the board with consecutive numbers, then shuffle it by swapping elements at random a certain number of times.

A good shuffling algorithm is https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

Sign up to request clarification or add additional context in comments.

1 Comment

Good solution, perhaps a bit of overkill.
1

Use srand() function instead of rand function. rand() will give same numbers after each program. Refer this for help. This is for improvement of your program this is not answer Rand() vs srand() function

Comments

0

Deleted since downvoted. That is a reason why it is removed.

3 Comments

but this is just a clumsy way to work around the inherent limitations of using the wrong design.
One problem with this approach is that you are assuming that a rand implementation draws numbers in its periodic cycle such that when the results are transformed under the function % 25, they completely fill the range [0, 24]. The C standard makes no such guarantees (a trivial counterexample would be a rand implementation that draws only even numbers), so this approach could cause an infinite loop. Granted, this is unlikely but it does illustrate an important flaw in this technique.
@Bathsheba: While it is not formally stated in the C standard, I am confident the intent of the standard is that rand is intended to select from the integers in [0, RAND_MAX] with uniform distribution. A rand that returned only even values (always, not just in some limited subsequence) would be defective.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.