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;
    }
  }
}
Rippo
  • 22,117
  • 14
  • 78
  • 117
Quicky App
  • 123
  • 2
  • 14
  • 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. – underscore_d Jan 31 '18 at 15:25
  • See https://stackoverflow.com/q/3343797/2410359 – chux - Reinstate Monica Jan 31 '18 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. – VladP Jan 31 '18 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: https://cboard.cprogramming.com/c-programming/141644-finding-distribution-rand-function.html – VladP Jan 31 '18 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. – Bathsheba Feb 01 '18 at 08:33

3 Answers3

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

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
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

0

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

VladP
  • 529
  • 3
  • 15
  • but this is just a clumsy way to work around the inherent limitations of using the wrong design. – underscore_d Jan 31 '18 at 15:28
  • 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 Jan 31 '18 at 15:47
  • 1
    @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. – Eric Postpischil Jan 31 '18 at 16:23