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

Java中如何生成指定长度的无重复唯一随机值Vector?

How to Generate a Vector of 100 Unique Random Gaussian Values

Hey there! Let's break down your problem and work through solutions that actually get you the unique values you need. First, let's understand why your current approaches aren't working:

Why Your Existing Methods Fall Short

  • Original Code Has Duplicates: When you round the absolute Gaussian value to one decimal place (Math.round(val*10)/10.0), you're severely limiting the pool of unique possible values. The absolute Gaussian distribution is heavily concentrated between 0 and 3—values beyond that are extremely rare. That leaves you with only ~31 unique possible values (0.0, 0.1, ..., 3.0), so duplicates are unavoidable when generating 100 entries.
  • Set Can't Reach 100 Values: Since there simply aren't enough unique one-decimal-place absolute Gaussian values to begin with, your Set will max out at around 31 entries, making it impossible to fill a 100-element Vector.

Practical Solutions to Fix This

Option 1: Skip Rounding (Simplest Fix)

If you don't strictly need rounded values, use the raw absolute Gaussian values directly. For continuous distributions like Gaussian, the probability of generating two identical double values is effectively zero. Here's the modified code:

Random rand = new Random();
Vector randomProjection = new Vector(100);
for(int d=0; d<100; d++) {
    double val = Math.abs(rand.nextGaussian());
    randomProjection.set(d, val);
}

This will give you 100 nearly guaranteed unique values with minimal changes to your original code.

Option 2: Keep Rounding but Increase Decimal Precision

If rounded values are a requirement, increase the number of decimal places to expand the pool of unique possible values. For example, using two decimal places gives you 500+ possible values (0.00 to 5.00, which covers almost all absolute Gaussian values), making it easy to collect 100 unique entries. Here's how to implement this with a Set:

Random rand = new Random();
Set<Double> uniqueVals = new HashSet<>();

// Keep generating until we hit 100 unique values
while(uniqueVals.size() < 100) {
    double val = Math.abs(rand.nextGaussian());
    double roundedVal = Math.round(val * 100) / 100.0; // Two decimal places
    uniqueVals.add(roundedVal);
}

// Convert the Set to your Vector
Vector randomProjection = new Vector(100);
int index = 0;
for(Double val : uniqueVals) {
    randomProjection.set(index++, val);
}

This loop will run quickly because the vast majority of Gaussian values fall within 0-5, giving you plenty of unique two-decimal options.

Option 3: Switch to Uniform Distribution (If Gaussian Shape Isn't Critical)

If you just need unique random values (and don't strictly require the Gaussian distribution), you can generate uniform random values in a wider range and round them—this guarantees you'll have enough unique values. For example:

Random rand = new Random();
Set<Double> uniqueVals = new HashSet<>();

while(uniqueVals.size() < 100) {
    // Generate a value between 0.0 and 10.0 with one decimal place
    double val = rand.nextDouble() * 10;
    double roundedVal = Math.round(val * 10) / 10.0;
    uniqueVals.add(roundedVal);
}

Vector randomProjection = new Vector(100);
int index = 0;
for(Double val : uniqueVals) {
    randomProjection.set(index++, val);
}

Since this covers 101 unique values (0.0 to 10.0), you'll easily hit your 100-entry target.


内容的提问来源于stack exchange,提问作者Shereen saleh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:01:11