# 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! # 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!

### 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  # JavaScript (ES6), 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!

### 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  # 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!

# 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! ### 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


# JavaScript (ES6), 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! ### 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


# JavaScript (ES6), 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! # JavaScript (ES6), 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!

### 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  2 saved 2 bytes # JavaScript (ES6), 8078 bytes Empty slots are _, mines are 7. f=(n,k=n,o=___ _${nk=n}_
___.replace(/_/g,c=>Math.random()<.5?--k|7:c))=>k?f(n):o


# JavaScript (ES6), 80 bytes

Empty slots are _, mines are 7.

f=(n,k=n,o=___
_${n}_ ___.replace(/_/g,c=>Math.random()<.5?--k|7:c))=>k?f(n):o  Try it online! # JavaScript (ES6), 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!

1