1

I'm new to programming and just started to learn c. I was hoping to create a simple battleship game but then I face a problem while generating random numbers. I wants to generate a random number that doesn't overlap with another range of number. Let's say I got 40 as my first random number and I don't want 40, 41, 42, 43 and 44 to appear again. So following is my code, I can't figure out what's wrong with it.

srand(time(NULL));
    for(counter = 0; counter < ship_num; counter++){
        checked = 0;
        while (!checked){
            ship_x[counter] = 1 + rand()%56;
            ship_y[counter] = 1 + rand()%20;
            checked = 1;
            for(check = 0; check < counter; check++){
                if(ship_y[counter] == ship_y[check]){
                    int check_x = ship_x[check] + 5;
                    if(ship_x[counter] >= ship_x[check] && ship_x[counter] <= check_x){
                        checked = 0;
                    }
                }
            }
        }
    }

All the variable is defined so I won't define it again here.

This is basically all the code that is used to generate the coordination of the ship.Output is like below.

0.Coordinates: 5, 47
1.Coordinates: 9, 10
2.Coordinates: 8, 55
3.Coordinates: 12, 51
4.Coordinates: 16, 48
5.Coordinates: 17, 32
6.Coordinates: 7, 24
7.Coordinates: 16, 35
8.Coordinates: 1, 7
9.Coordinates: 7, 36
10.Coordinates: 11, 54
11.Coordinates: 17, 29
12.Coordinates: 6, 24
13.Coordinates: 11, 8
14.Coordinates: 3, 5
15.Coordinates: 2, 5
16.Coordinates: 14, 21
17.Coordinates: 20, 24
18.Coordinates: 4, 18
19.Coordinates: 14, 19
20.Coordinates: 14, 1
21.Coordinates: 13, 48
22.Coordinates: 18, 43
23.Coordinates: 16, 25
24.Coordinates: 4, 30
25.Coordinates: 10, 3
26.Coordinates: 18, 17
27.Coordinates: 4, 56
28.Coordinates: 4, 9
29.Coordinates: 1, 4
30.Coordinates: 14, 29
31.Coordinates: 3, 27
32.Coordinates: 18, 56
33.Coordinates: 9, 9
34.Coordinates: 1, 24
35.Coordinates: 7, 42
36.Coordinates: 5, 3
37.Coordinates: 14, 27
38.Coordinates: 4, 50
39.Coordinates: 15, 8
40.Coordinates: 5, 36
41.Coordinates: 6, 37
42.Coordinates: 14, 44
43.Coordinates: 12, 21
44.Coordinates: 1, 49
45.Coordinates: 17, 41
46.Coordinates: 3, 24
47.Coordinates: 10, 2
48.Coordinates: 12, 13
49.Coordinates: 7, 32
50.Coordinates: 5, 11
51.Coordinates: 5, 10
52.Coordinates: 2, 36
53.Coordinates: 11, 29
54.Coordinates: 1, 45
55.Coordinates: 20, 40
56.Coordinates: 2, 52
57.Coordinates: 19, 28
58.Coordinates: 10, 34
59.Coordinates: 10, 31
60.Coordinates: 13, 18
61.Coordinates: 4, 39
62.Coordinates: 8, 33
63.Coordinates: 13, 26
64.Coordinates: 20, 10
65.Coordinates: 16, 18
66.Coordinates: 18, 35
67.Coordinates: 6, 13
68.Coordinates: 6, 34
69.Coordinates: 6, 30
70.Coordinates: 4, 49
71.Coordinates: 3, 14
72.Coordinates: 6, 8
73.Coordinates: 6, 19
74.Coordinates: 14, 11
75.Coordinates: 6, 55
76.Coordinates: 15, 36
77.Coordinates: 16, 15
78.Coordinates: 7, 31
79.Coordinates: 20, 3

As can see from above, coordinates 68 and 69 is the output that I didn't wish to see, it's too close together.

Never mind guys, Thanks for all the comment. I think I just found out what's wrong with my code. I remove all the number that is 1 to 4 number bigger than the one I have in my array but I forgot to remove the one that is smaller, that's why it keeps appearing. My new code just solve it, here it is and i hope it can help someone else who face the same problem.

srand(time(NULL));
    for(counter = 0; counter < ship_num; counter++){
        checked = 0;
        while (checked == 0){
            ship_x[counter] = 1 + rand()%56;
            ship_y[counter] = 1 + rand()%20;
            checked = 1;
            for(check = 0; check < counter; check++){
                if(ship_y[counter] == ship_y[check]){
                    if(ship_x[counter] <= ship_x[check] + 5 && ship_x[counter] >= ship_x[check] - 5){
                        checked = 0;
                    }
                }
            }
        }
    }
Rui Dian
  • 75
  • 6
  • 4
    Treat it like a deck of cards: start with a pool of all the options, pick one at random, remove it (and other now unwanted numbers) from the pool, reduce the number of options. – Weather Vane Oct 28 '17 at 12:22
  • It's better to put all the relevant code in as your definitions may give a clue as to what is wrong. Also, include the 'wrong' output. – Nick Oct 28 '17 at 12:22
  • If your data set is small enough, do as @WeatherVane said. If you think your data set is too big for enumeration, check whether the new value is unsatisfactory and simply generate a new coordinate and repeat. Just make sure that there are some options available. If you have a list of acceptable options and it gets to empty, you know there’s no point in continuing. – Jonathan Leffler Oct 28 '17 at 12:59
  • Related: https://stackoverflow.com/q/5731863/694576 – alk Oct 28 '17 at 13:13
  • @JonathanLeffler that's my problem. I tried to check the value with the code if(ship_x[counter] >= ship_x[check].... but then I have no idea why it doesn't work. – Rui Dian Oct 28 '17 at 13:53
  • You’ve not shown the printing that produces the output you show. Please creat an MCVE ([MCVE]). It might only be 10 more lines of code; it certainly shouldn’t be much bigger than what you’ve got. – Jonathan Leffler Oct 28 '17 at 13:59

0 Answers0