[Question]
  [
[Minesweeper](https://en.wikipedia.org/wiki/Minesweeper_(video_game)) is a puzzle game where mines are hidden around a board of nondescript tiles with the goal of identifying the location of all mines. Clicking on a mine loses the game, but clicking on any other tile will reveal a number from 0-8 which signifies how many mines directly surround it.
Given a number, you must display a random\* possible combination of empty tiles and mines surrounding it. This should be in the form of a 3x3 array. The center tile should be the number of mines taken as input.
\*Must have a non-zero chance for all combinations to occur.
---
## Examples
```
_ = blank square
X = mine
0
___
_0_
___
1
_X_
_1_
___
1
___
_1_
X__
___
_1_
__X
4
_X_
X4X
_X_
4
X_X
_4_
X_X
4
___
X4X
X_X
8
XXX
X8X
XXX
```
---
### Input
* The number of mines surrounding the center tile (0-8)
---
### Output
* Any reasonable form of output that displays the 3x3 array of tiles
---
## Other rules
* Each combination does not have to have an equal chance of happening. There just must be a non-zero chance of each combination to occur when executing your program.
* Any 2 characters can be chosen for the mine and empty tile.
* This is code golf, the program with fewest bytes wins.
  
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 28 15 bytes
-13 bytes thanks to ngn!
```
{3 3⍴5⌽⍵,⍵≥8?8}
```
## Explanation:
`{...}` A direct function (D-Fn), `⍵` is its right argument.
`8?8` deal 8 random numbers from the list 1..8:
```
      8?8                         
7 2 1 8 4 6 5 3
```
`⍵≥` is the argument greater or equal to each of them?:
```
      5 ≥ 7 2 1 8 4 6 5 3   
0 1 1 0 1 0 1 1
```
`⍵,` prepend the argument to the boolean list:
```
      5 , 0 1 1 0 1 0 1 1
5 0 1 1 0 1 0 1 1
```
`5⌽` rotate the list 5 positions to the left, so that the argument is in the center:
```
      5 ⌽ 5 0 1 1 0 1 0 1 1
1 0 1 1 5 0 1 1 0
```
`3 3⍴` reshape the list to a 3x3 matrix:
```
      3 3 ⍴ 1 0 1 1 5 0 1 1 0
1 0 1
1 5 0
1 1 0
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R24RqYwXjR71bTB/17H3Uu1UHiB91LrWwt6gFSisYc6nrKgChOleagikS2xwA "APL (Dyalog Unicode) – Try It Online")
# [J](http://jsoftware.com/), 15 bytes
Also many bytes thanks to ngn!
```
3 3$5|.],]>8?8:
```
[Try it online!](https://tio.run/##y/qfVmyrp2CgYKVg8N9YwVjFtEYvVifWzsLewuq/JpeSnoJ6mq2euoKOQq2VQloxF1dqcka@QpqCEYShrqsAhOowUROsoub/AQ "J – Try It Online")
[Answer]
# JavaScript (ES6), 67 bytes
*Shorter version suggested by @tsh*
Empty slots are `0`, mines are `1`.
```
n=>`___
_${t=9,n}_
___`.replace(/_/g,_=>n-(n-=Math.random()<n/--t))
```
[Try it online!](https://tio.run/##DcpBDoIwEEDRPaeYhUnblIJLFYYbeAOToYGCEpyS0rghnL129d/iL/Zn9yF8tmjYjy5NmBi7nogKuhwR7yWfmUR9Fdy22sHJmuq5JOzYSDb4tPFdBcuj/0rVcm1MVCpNPkgGhGsDDC3CLVdrBUcBMHje/eqq1c/50SAeLxa5k2SlRbZqijP9AQ "JavaScript (Node.js) – Try It Online")
---
# Original trial-and-error version, 78 bytes
Empty slots are `_`, mines are `7`.
```
f=(n,o=`___
_${k=n}_
___`.replace(/_/g,c=>Math.random()<.5?--k|7:c))=>k?f(n):o
```
[Try it online!](https://tio.run/##FYtBDoIwEADvvGIPJrQpFC9GAxRe4A9MalMKKrhLCvGCvL3W08xh5mU@ZrH@Oa85UudC6BXDjNRda53owzYq3KNofZfezZOxjhW6GDKrmqtZH9Ib7OjNeC1PbZ6P33NpOVfN2PYMeUmhJ88QFBwrQKgVXCKF4LAlAJZwocnJiYbYCEjLG6aR/1Wk0XmV7OEH "JavaScript (Node.js) – Try It Online")
### Commented
```
f = (                         // f = recursive function
  n,                          // n = input
  o = `___\n_${k = n}_\n___`  // o = minesweeper field / k = backup of n
    .replace(/_/g, c =>       // for each underscore character c in o:
      Math.random() < .5 ?    //   random action:
        --k | 7               //     either decrement k and yield 7
      :                       //   or:
        c                     //     let the underscore unchanged
    )                         // end of replace()
) =>                          //
  k ?                         // if k is not equal to 0:
    f(n)                      //   try again
  :                           // else:
    o                         //   stop recursion and return o
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~13~~ 10 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
8Ẉ>RẊs4js3
```
The returned list of lists has the displayed integer in the centre surrounded by 0s and 1s representing mines and blanks respectively.
**[Try it online!](https://tio.run/##y0rNyan8/9/i4a4Ou6CHu7qKTbKKjf8fbo/8/98YAA "Jelly – Try It Online")** (footer pretty prints the array)
### How?
```
8Ẉ>RẊs4js3 - Link: integer, n                   e.g.  3
8          - eight                                    8
 Ẉ         - length of each (implicit range of eight) [1,1,1,1,1,1,1,1]
   R       - range of n                               [1,2,3]
  >        - greater than? (vectorises)               [0,0,0,1,1,1,1,1]
    Ẋ      - shuffle                                  [1,1,1,0,0,1,1,0]
     s4    - split into chunks of 4                   [[1,1,1,0],[0,1,1,0]]
       j   - join (with n)                            [1,1,1,0,3,0,1,1,0]
        s3 - split into chunks of 3                   [[1,1,1],[0,3,0],[1,1,0]]
```
[Answer]
# Pyth, ~~16~~ 14 bytes
```
c3jQc2.S.[d8*N
```
Saved 2 bytes thanks to isaacg.  
Uses spaces for safe spots and quotes for mines.  
[Try it here](https://pyth.herokuapp.com/?code=c3jQc2.S.%5Bd8%2aN&input=2&debug=0)
### Explanation
```
c3jQc2.S.[d8*N
            *NQ     Get (implicit) input number of quotes...
        .[d8        ... and pad to length 8 with spaces.
      .S            Shuffle.
  jQc2              Stick the input in the middle.
c3                  Split into three.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 9 bytes
```
<Ɱ8Ẋs4js3
```
[Try it online!](https://tio.run/##y0rNyan8/9/m0cZ1Fg93dRWbZBUb/z/c7v7/vzEA "Jelly – Try It Online")
empty = `1`  
mine = `0`
Note that `1` and `0` are integers.
Another note: this is somewhat similar to Jonathan Allan's 10-byte answer, but it's not really influenced by it in any way, and the mechanism, if you pay close attention, is actually more different than at first glance.
[Answer]
# Oracle 18 SQL, 230 bytes
Not a golfing language but...
```
WITH v(v)AS(SELECT*FROM SYS.ODCINUMBERLIST(0,1))SELECT v.v||b.v||c.v||'
'||d.v||n||e.v||'
'||f.v||g.v||h.v
FROM n,v,v b,v c,v d,v e,v f,v g,v h
WHERE v.v+b.v+c.v+d.v+e.v+f.v+g.v+h.v=n
ORDER BY DBMS_RANDOM.VALUE
FETCH NEXT ROW ONLY
```
The input value is in a table `n` with column `n`:
```
CREATE TABLE n(n) AS
SELECT 7 FROM DUAL;
```
Try it online - log onto <https://livesql.oracle.com> and paste it into a worksheet.
Output:
```
V.V||B.V||C.V||''||D.V||N||E.V||''||F.V||G.V||H.V
-------------------------------------------------
101 
171 
111
```
To get all possible combinations (183 Bytes):
```
WITH v(v)AS(SELECT*FROM SYS.ODCINUMBERLIST(0,1))SELECT v.v||b.v||c.v||'
'||d.v||n||e.v||'
'||f.v||g.v||h.v
FROM n,v,v b,v c,v d,v e,v f,v g,v h
WHERE v.v+b.v+c.v+d.v+e.v+f.v+g.v+h.v=n
```
Output:
```
V.V||B.V||C.V||''||D.V||N||E.V||''||F.V||G.V||H.V
-------------------------------------------------
111 
171 
110
111 
171 
101
111 
171 
011
111 
170 
111
111 
071 
111
110 
171 
111
101 
171 
111
011 
171 
111
```
[Answer]
# Japt, 13 bytes
```
çÊú8 öÊi4U ò3
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.5&code=58r6OCD2ymk0VSDyMw==&input=NAotUg==)
---
## Explanation
```
                   :Implicit input of integer U
 Ê                 :"l"
ç                  :Repeat U times
  ú8               :Pad right to length 8
    öÊ             :Random permutation
       i4U         :Insert U at index 4
            ò3     :Split at every 3rd character
```
[Answer]
# [QBasic 1.1](http://www.qbasic.net/), ~~206~~ 186 bytes
```
RANDOMIZE TIMER
INPUT N
O=N
Z=8-N
FOR I=0TO 7
IF O*Z THEN
R=RND<.5
O=O+R
Z=Z-1-R
A(I)=-R
ELSEIF O THEN
O=O-1
A(I)=1
ELSE
Z=Z-1
A(I)=0
ENDIF
NEXT I
?A(0)A(1)A(2)
?A(3)N;A(4)
?A(5)A(6)A(7)
```
-20 thanks to [DLosc](https://codegolf.stackexchange.com/users/16766/dlosc) (newly-posted golfing trick).
Empty = `0`  
Mine = `1`
Note that `0` and `1` are integers, but I'm using STDOUT anyway, so...
Output appears like this:
```
 A  B  C
 D  x  E
 F  G  H
```
Where A-H are 0/1 and x is the input.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 19 bytes
```
Ｗ¬⁼ΣＩＫＡ⁸«Ｅ³⭆³‽²↑↗↖θ
```
[Try it online!](https://tio.run/##S85ILErOT8z5/788IzMnVUHDL79Ew7WwNDGnWCO4NFfDObG4RCMgNTXbMSdHQ1NTU0fBAkgqVHNxBhRl5pVo@CYWaBjrKASXAHnpUE5QYl5Kfq6GEVChpjUXp29@WaqGVWgBMjsoMz2jBCQAMQUo4pOaVqKjUAgUq/3/3@S/blkOAA "Charcoal – Try It Online") Link is to verbose version of code. Uses `0` for a mine, `1` for an empty space. Explanation:
```
     ＫＡ             Peek the entire canvas
    Ｉ               Cast to integer
   Σ                Take the sum
       ⁸            Literal 8
  ⁼                 Equals
 ¬                  Logical Not
Ｗ       «           While
          ³ ³       Literal 3
         Ｅ          Map over implicit range
           ⭆        Map over implicit range and join
              ²     Literal 2
             ‽      Random element of implicit range
                    Implicitly print on separate lines
               ↑↗↖θ Print the original input in the middle
```
* `Peek` returns an array of strings, which `Sum` simply concatenates, which is why we have to cast to integer first. (`Sum(Sum(PeekAll()))` also works.)
* `Sum` returns `None` for an empty array (first loop), so the only safe comparison is `Not(Equals(...))`.
* Nilary `Random` always returns `0`, although its documentation says otherwise.
Alternative solution, was 19 bytes, now 17 bytes after a Charcoal bugfix:
```
θ←9Ｗ⁻ΣＩＫＡ⁸ＵＭＫＭＩ‽²
```
[Try it online!](https://tio.run/##HcoxDsIwDEDRnVN4tKWwIAZEJ9Q5UgUniEqkRjhxmyZwfGN1/f/NS6izBFb1nVtaayoN7w9mBxP3HTcHG9Fw@i2JI6BPxeKrZxzD3nCK8WMWicjBjQh8WEfJOZT38bxIjWjv0E/LkvFielC96vnLfw "Charcoal – Try It Online") Link is to verbose version of code. Uses `0` for a mine, `1` for an empty space. Explanation:
```
θ
```
Print the original input.
```
←9
```
Print a `9` leftwards. This moves the cursor back over the original input, and also forces at least one iteration of the while loop (otherwise an input of `8` would do nothing).
```
Ｗ⁻ΣＩＫＡ⁸
```
Repeat while the difference between the sum of all the digits on the canvas and 8 is nonzero:
```
ＵＭＫＭＩ‽²
```
Replace each of the surrounding characters randomly with `0` or `1`.
[Answer]
# [R](https://www.r-project.org/), ~~67 63 62~~ 59 bytes
```
matrix(c(sample(rep(1:0,c(n<-scan(),8-n))),n),6,5)[2:4,1:3]
```
[Try it online!](https://tio.run/##K/r/PzexpCizQiNZozgxtyAnVaMotUDD0MpAJ1kjz0a3ODkxT0NTx0I3T1NTUydPU8dMx1Qz2sjKRMfQyjj2v@l/AA "R – Try It Online")
Uses `1` and `0`. Build a `n* 1 +(8-n)* 0` vector, shuffles it, appends `n`, builds the bigger matrix shown below(where `a...i` stand for the elements of the original vector) and extracts the proper sub-matrix shown in uppercase:
```
     [,1] [,2] [,3] [,4] [,5]
[1,] "a"  "g"  "d"  "a"  "g" 
[2,] "B"  "H"  "E"  "b"  "h" 
[3,] "C"  "I"  "F"  "c"  "i" 
[4,] "D"  "A"  "G"  "d"  "a" 
[5,] "e"  "b"  "h"  "e"  "b" 
[6,] "f"  "c"  "i"  "f"  "c"
```
[Answer]
# [Python 2](https://docs.python.org/2/), 93 bytes
```
from random import*
def f(n):a=[1]*n+[0]*(8-n);shuffle(a);a[4:4]=[n];return zip(*[iter(a)]*3)
```
[Try it online!](https://tio.run/##FY27DgIhEAB7v2JLFmPi4wqF3JcQCpJjvU28haxcoT@PWE0xyUz9tLXItXfSsoEmWQZ4q0WbPSyZgIygS3O4RCvHcI7W3E@C/r3uRK9sEvoUJjfFOUj0mtuuAl@uxgZuWYeP9oadigIDy//wzOaBDqqytJFn7D8 "Python 2 – Try It Online")
`0` for empty; `1` for a mine; and `n` for one's self.
[Answer]
# [Attache](https://github.com/ConorOBrien-Foxx/Attache), 51 bytes
```
{[3,3]&Rotate[Sample[{Sum@_=_2}&_\BinBelow@8]'_,4]}
```
[Try it online!](https://tio.run/##SywpSUzOSP2fZmX7vzraWMc4Vi0ovySxJDU6ODG3ICc1ujq4NNch3jbeqFYtPsYpM88pNSe/3MEiVj1exyS29n9JcnRerIKVrYJGQFFmXkl0no6Ckq6dUqyCtbWCS2ZxQU5iZXQaUAlYAKIkVpOrJFnB1k7BwMriPwA "Attache – Try It Online")
## Explanation
Similar to [Galen's J/APL answer](https://codegolf.stackexchange.com/a/166070/31957), the basic technique is to generate an array of 1s and 0s with the correct number of mines, inserting the input by appending it to the end, rotating the array such that the input lies in the center, then reshaping into a 3x3 matrix.
### Part 1: generating the binary array
There are many ways to go about doing this, but I mainly happened across two types: brute force and selection.
The primary brute force method looks like this:
```
NestWhile[{Random[8&2]},0,{Sum@_/=_2}&_]
```
This generates random arrays of 8 binary digits (`Random[8&2]`) while their sums do not equal the input `{Sum@_/=_2}&_`. This is a bit verbose, as the following emphasized portions of the code are there "just to make it work":
```
NestWhile[{Random[8&2]},0,{Sum@_/=_2}&_]
          ^           ^^^^^        ^^^^
```
And I discarded the idea.
Selection is more interesting. The main concept is to use the `BaseBelow[b, n]` builtin, which generates a list of all base-`b` integers of width `n` (as digit arrays), from `0` to `b^n-1`. For example, `BaseBelow[3, 2]` generates all ternary integers of width 2:
```
A> BaseBelow[3, 2]
 0 0
 0 1
 0 2
 1 0
 1 1
 1 2
 2 0
 2 1
 2 2
```
We specifically use `BaseBelow[2, 8]` for all binary integers of width 8. These represent all possible minefields of all lengths. This is the first step.
The second step is selecting all such arrays with only `N` 1s, where `N` is the input. My first idea was to translate this English statement directly into Attache:
```
Chunk[SortBy[Sum,BaseBelow[2,8]],Sum]@N@1
```
However, not only did this come out to be 1 byte longer than the aforementioned approach, it's also highly repetitive—and it isn't even randomized yet!. Sure, I could probably save 1 byte by reorganizing how `BaseBelow` is called, but it simply isn't worth using the approach.
So I decided to kill two birds with one stone and use a `Shuffle` based approach. The following gives all valid minefields of length `N` in random order:
```
{Sum@_=_2}&N\Shuffle[BaseBelow&8!2]
```
Then, all that needs to be done is to select the first. But I can do better—surely it'd be better to simply `Sample` the filtered array? This approach comes out to be something like this:
```
Sample[{Sum@_=_2}&_\BaseBelow[2,8]]
```
I had to revert the `BaseBelow&8!2` golf because `\`s precedence is too high. Otherwise satisfied, I proceeded to chop a byte off of that:
```
Sample[{Sum@_=_2}&_\2&BaseBelow@8]
```
(I discovered another way to succinctly call a dyadic function here: `x&f@y` is a high-precedence expression which evaluates to `f[x, y]`.)
However, despite this, I remembered that, all along, an alias for `2&BaseBelow` existed: `BinBelow`. So I used that:
```
Sample[{Sum@_=_2}&_\BinBelow@8]
```
This generates the desired minefield. I'm convinced this is near optimal.
### Part 2: Forming the array
As said earlier, the forming technique I used is similar to the J/APL answer, so I won't go into too much detail. Suppose `MINEFIELD` is the result from the last section. The function then becomes:
```
{[3,3]&Rotate[MINEFIELD'_,4]}
```
`MINEFIELD'_` concatenates the minefield with the original input `_`, giving us something like this:
```
[1, 0, 0, 0, 1, 0, 0, 1, 3]
```
Then, `Rotate[MINEFIELD'_,4]` rotates this list `4` times to the left, placing the center:
```
[1, 0, 0, 1, 3, 1, 0, 0, 0]
```
The last step is using `[3,3]&` to reshape the list into a 3x3 matrix:
```
 1 0 0
 1 3 1
 0 0 0
```
[Answer]
# Java 10, ~~165~~ ~~157~~ 141 bytes
```
n->{var r="___\n_"+n+"_\n___";for(int i;n>0;r=r.charAt(i*=Math.random())>58?r.substring(0*n--,i)+(i>9?0:0+r.substring(i+1)):r)i=11;return r;}
```
Empty tiles are `_` (any character with a unicode value above 58 is fine) and mines are `0`.
[Try it online.](https://tio.run/##TZBBbsIwEEX3PcUoKxs3UbJAorgO6gFgw7JUkQmhDIUJjB2kCnH21CRpqWRbHn/rz/uztxcb7zdfbXmwzsHcIl2fAJB8xVtbVrC4lwBLz0ifUIqgAEkdHm9hh@W89VjCAggMtBTn14tlYBMVRbGiIlKkovulKCK9rbkzQE15qtlwUu4sv3mBIzO3fpewpU19FFLm48mME9esXddYpCOK42eUSmD@MkunqfqvosqknLJEk2WaK98wAetbq3vEU7M@BMSB9FLjBo4hqOhDvX@AlX1KXzkv0i4dwB@syTTg6yQcSslOeohnM9ZwjuMQB361wQcHn66YDCN7DKzD6D/emwwEy2/nq2NSNz45BTh/IEFJmLpU0YqiweTW/gA)
**Explanation:**
```
n->{                           // Method with integer parameter and String return-type
  var r="___\n_"+n+"_\n___";   //  Result-String, starting at:
                               //   "___
                               //    _n_
                               //    ___"
  for(int i;n>0                //  Loop as long as `n` isn't 0 yet:
      ;                        //    After every iteration:
       r=r.charAt(i*=Math.random())
                               //     Select a random integer in the range [0,11)
         >58?                  //     And if the character at this random index is a '_':
          r.substring(0*n--,i) //      Set `r` to the first part excluding character `i`,
                               //      (and decrease `n` by 1 in the process)
          +(i>9?0:0+           //      appended with a '0',
           r.substring(i+1))   //      appended with the second part
         :                     //     Else:
          r)                   //      Leave `r` unchanged
     i=11;                     //   Set `i` to 11 so a new random integer can be chosen
  return r;}                   //  Return the result
```
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 77 bytes
```
param($n)-join(0..7|random -c 8|%{'X_'[$_-ge$n]
$n[++$i-ne4]
'
'*($i-in3,5)})
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfmvkqZgq1D9vyCxKDFXQyVPUzcrPzNPw0BPz7ymKDEvJT9XQTdZwaJGtVo9Il49WiVeNz1VJS@WSyUvWltbJVM3L9UklkudS11LA8jJzDPWMdWs1fxfy8UFNAGki0sBCNSAlqjEg5nq6ly1/wE "PowerShell – Try It Online")
[Answer]
# [PHP](https://php.net/), ~~135~~ ~~134~~ ~~123~~ ~~117~~ ~~122~~ 121 bytes
Looping over str to print instead saves 1 byte
str\_split and implode to insert the centre number saves 11 bytes
~~Don't need to assign the string to $s anymore saving 6 bytes~~  
Yes you do. Otherwise the string is shuffled after every echo...
Removing whitespace after echo saves 1 byte
Replacing "\n" with an ctual line break saves 1 byte
```
$n=$argv[1];$s=implode($n,str_split(str_shuffle(str_pad(str_repeat(m,$n),8,n)),4));for(;$i<9;)echo$s[$i].(++$i%3?"":"
");
```
[Try it online!](https://tio.run/##HcZBCsIwEEDRvccII2RoFKQu1LT0CB6glBJs2gTazJBEj2@Ubv5/7Lg0Hf8LoQUTl09/GTSk1m@80mQlBJVyHBOvPstd7j3Pq93NZtofLVuT5aYgoLqpgKiuiHqmKDX45q7RvhxB6sEPZ1lV4I91J8RDHATqUkr9Jc6eQiqnKTmKeSS2YcxmaZ/hBw "PHP – Try It Online")
[Answer]
# [Alice](https://github.com/m-ender/alice), 24 bytes
```
a8E/be?SZ!@
~E-\ita*.o/a
```
[Try it online!](https://tio.run/##S8zJTE79/z/RwlU/KdU@OErRgavOVTcmsyRRSy9fP/H/f2MA "Alice – Try It Online")
Empty slots are `9`, and mines are `0`.
I also made a 27-byte program with `1` for mines and `0` for empty slots:
```
a8Ea8/be?SZ!@
-E-9:\ita*.o/
```
[Try it online!](https://tio.run/##S8zJTE79/z/RwjXRQj8p1T44StGBS9dV19IqJrMkUUsvX///f2MA "Alice – Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 81 bytes
```
f(n,i,j,k){for(j=i=8,k=n;--i+5;putchar(i%4?i-2?rand()%j--<k?k--,88:95:48+n:10));}
```
[Try it online!](https://tio.run/##VcxBCoMwEADAu68IgrBLsmCLQjQG3yIptpvQWFJ7Et@e5lToeWAc3Z3LeYWoWHkV8Fi3BN6y1SrYaIhY9ub12d1jScBNNzNd57TEG2DjiaYwByKl9Tj0Y6dlHC8tojnzc@EIeFSlE8BxF2xbw9NgWEpVvjfUNWIlfu6L@@L@38UKjKY68xc "C (gcc) – Try It Online")
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), ~~91~~ 86 bytes
-5 bytes thanks to mazzy
```
param($n)$x=,'X'*$n+,'_'*(8-$n)|random -c 8
-join$x[0..2]
$x[3,4]-join$n
-join$x[5..7]
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfmvkmZb/b8gsSgxV0MlT1OlwlZHPUJdSyVPW0c9Xl1Lw0IXKFpTlJiXkp@roJusYMGlm5WfmadSEW2gp2cUywVkGOuYxEIE8@CSpnp65rH/a7m4gKosalSruTjVVNIUVOK5OJWUuDhr/wMA "PowerShell – Try It Online")
Shuffles a generated string ranging from `________` to `XXXXXXXX` (replacing from the left). It then slices through it multiple times, inserting `$n` in the middle, to build the output string. This last part can probably be greatly optimized because each index costs a minimum of 5 bytes.
[Answer]
# [Ruby](https://www.ruby-lang.org/), 59 bytes
Returns a list of strings (one for each line).
```
->n{a=([0]*(8-n)+[1]*n).shuffle;a[4,0]=n;a.join.scan /.../}
```
[Try it online!](https://tio.run/##BcFNCoUgFAbQrTTUfr4MGgRhGzEH90WSEbfIHES1dt85Z/zdyekxVQM/pIVRNhddxbIwjc1ZIizRuW3uybSlspp7wrp7RpiIsxpA/SWhgE5ipml5Xv8e8QqZM96Wx5f@ "Ruby – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/) `-pa`, ~~105~~ 101 bytes
```
@o=grep!/^....1/&&y/1//=="@F",map{sprintf"%09b",$_}0..2**9-1;$_=$o[rand@o];s/....\K./@F/;s/...\K/\n/g
```
[Try it online!](https://tio.run/##K0gtyjH9/98h3za9KLVAUT9ODwgM9dXUKvUN9fVtbZUc3JR0chMLqosLijLzStKUVA0sk5R0VOJrDfT0jLS0LHUNrVXibVXyo4sS81Ic8mOti/VBRsR46@k7uOlDeDHe@jF5@un//5v8yy8oyczPK/6v62uqZ2Bo8F@3IBEA "Perl 5 – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 12 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
$×8j.r2äIý3ô
```
Uses `0` for mines, spaces for blank squares. Outputs a list of lines, which is pretty printed in the TIOs below by joining with newline delimiter (`»`).
[Try it online](https://tio.run/##yy9OTMpM/f9f5fB0iyy9IqPDSzwP7zU@vOX/od3/TQA) or [verify a few more test cases at once](https://tio.run/##ATAAz/9vc2FiaWX/N0w10Lh7MMWhOMKqdnn/MHnDlzhqLnIyw6R5w70zw7T/PcK7LMK2P/8).
**Explanation:**
```
$             # Push 0 and the input-digit
 ×            # Repeat the 0 the input-digit amount of times as string
              #  i.e. 4 → "0000"
  8j          # Prepend spaces to make the size 8
              #  → "    0000"
    .r        # Randomly shuffle the characters in this string
              #  i.e. "    0000" → " 00 0  0"
      2ä      # Split it into two equal halves (of 4 characters)
              #  → [" 00 ","0  0"]
        Iý    # Join it with the input-digit
              #  → " 00 40  0"
          3ô  # And then split it into (three) parts of 3 characters
              #  → [" 00"," 40","  0"]
              # (which is output implicitly as result)
```
**12 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage) alternative:**
```
[[email protected]](/cdn-cgi/l/email-protection)š5._3ô
```
Uses `1` for mines, `0` for blank squares. Outputs a matrix of digits, which is pretty printed in the TIOs below by joining each row, and then these rows with newline delimiter (`J»`).
[Try it online](https://tio.run/##yy9OTMpM/f/fwsdBr@hRw6zTc0z14o0Pb/n/3xgA) or [verify a few more test cases at once](https://tio.run/##ATIAzf9vc2FiaWX/N0w10Lh7MMWhOMKqdnnDkP84TEAucuKAmsucNS5fM8O0/z1KwrsswrY//w).
**Explanation:**
```
8L            # Push the list [1,2,3,4,5,6,7,8]
  @           # Check for each if the (implicit) input-integer is >= it
              # (1 if truthy; 0 if falsey)
              #  i.e. 4 → [1,1,1,1,0,0,0,0]
   .r         # Randomly shuffle this list
              #  i.e. [1,1,1,1,0,0,0,0] → [0,1,1,0,1,0,0,1]
     Iš       # Prepend the input-digit to the list
              #  → [4,0,1,1,0,1,0,0,1]
       5._    # Rotate the list five times towards the left
              #  → [1,0,0,1,4,0,1,1,0]
          3ô  # And then split it into (three) parts of 3 digits each
              #  → [[1,0,0],[1,4,0],[1,1,0]]
              # (which is output implicitly as result)
```
]