You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何生成偏向取值边界的随机数?战舰AI船只放置实现需求

Got it, let's figure out how to make your Battleship AI place ships closer to the edges like those winning stats suggest. The key is generating random numbers that favor the ends of your 0-9 range (and we can extend this to any range too). Here's what you can do:

1. Solution for Your 0-9 Battleship Board

We need a random number generator where values near 0 and 9 are more likely to pop up than middle values like 4 or 5. There are two straightforward approaches:

Option 1: Mathematical Transformation (Smooth Bias)

This method uses a mathematical transformation on a uniform random number to shift probability toward the edges. Here's a Python implementation:

import random

def edge_biased_0_9():
    # Generate a uniform random float between 0 and 1
    u = random.random()
    
    if u < 0.5:
        # Bias toward the 0 end: squaring concentrates values near 0
        transformed = (2 * u) ** 2
    else:
        # Bias toward the 9 end: mirror the left side to concentrate near 1 (which maps to 9)
        transformed = 1 - (2 * (1 - u)) ** 2
    
    # Convert the transformed value to an integer between 0 and 9
    return int(transformed * 10)

When you run this, numbers like 0 and 9 will appear most frequently, with probabilities dropping off as you move toward the center of the range.

Option 2: Weighted Random Selection (Controllable Bias)

If you want more control over how strong the edge bias is, use weighted choices. Assign higher weights to edge values and lower weights to middle values:

import random

def weighted_edge_biased_0_9():
    # Weights: higher values = more likely to be chosen
    # Adjust these numbers to make the bias stronger/weaker
    position_weights = [5, 4, 3, 2, 1, 1, 2, 3, 4, 5]
    return random.choices(range(10), weights=position_weights)[0]

For example, if you want an even stronger edge bias, bump the weights for 0 and 9 up to 10 and leave middle weights at 1.

2. Extending to Any Numeric Range

If you ever need this for a different board size (say, 0-14) or a custom interval [a, b], here's how to adapt the mathematical approach:

import random

def edge_biased_int(a, b):
    u = random.random()
    
    if u < 0.5:
        transformed = (2 * u) ** 2
    else:
        transformed = 1 - (2 * (1 - u)) ** 2
    
    # Map the transformed 0-1 value to the [a, b] integer range
    # Adding 1 ensures we include the upper bound `b`
    return a + int(transformed * (b - a + 1))

For floating-point values, just skip the integer conversion step:

def edge_biased_float(a, b):
    u = random.random()
    
    if u < 0.5:
        transformed = (2 * u) ** 2
    else:
        transformed = 1 - (2 * (1 - u)) ** 2
    
    return a + transformed * (b - a)
Quick Test Tip

To verify the bias works, run the function 1000+ times and count how often each number appears. You should see a clear trend: edge values pop up way more than middle ones.

内容的提问来源于stack exchange,提问作者Daniel Oliveira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:42:44