带权重的随机点名

package dianamingq;

import java.io.*;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class test2 {
    public static void main(String[] args) throws IOException {
        //带权重的随机点名器
        //首先是要读取文件信息
        BufferedReader br = new BufferedReader(new FileReader("ttt.txt"));
        String s ;
        ArrayList<student> a1 = new ArrayList<student>();
        while ((s=br.readLine())!=null)
        {
            String[] st= s.split("-");
            //用集合将他们拿出来,用student来存储
            a1.add(new student(st[0],st[1],st[2],st[3]));
        }
        br.close();
        //计算权重和,然后再相加求各个数值在数轴的占比
        int sum=0;
        for (int i = 0; i < a1.size(); i++) {
            sum+=a1.get(i).getWeight();
        }
        //然后把所有数据的权重除以总权重。得到他们再数值中的位置,定义一个数组记录
        Double []arr1 = new Double[a1.size()];
        for (int i = 0; i < arr1.length; i++) {
            if(i!=0){
            arr1[i]=a1.get(i).weight/sum+arr1[i-1];}else{
                arr1[i]=a1.get(i).weight/sum;
            }
        }
        //此时随机点名,生成一个0到1的随机数,在哪个区间,就点哪个人的名字
        Random ran = new Random();
        Double ran1 = ran.nextDouble(1e10+1)/1e10;
        System.out.println(ran1);
        //使用二分查找,就能确定应该插入那个位置
        int index = -Arrays.binarySearch(arr1,ran1) -1;  //该方法返回的是-index-1,我们要获得index
        //这个时候返回的就是我们要点的名字的索引了
        System.out.println(a1.get(index).getName());
        //要求是点到一次名字,他的权重就要减半
        a1.get(index).setWeight(a1.get(index).getWeight()/2);
        System.out.println(a1);
        BufferedWriter bw = new BufferedWriter(new FileWriter("ttt.txt"));
        //接下来把集合写回去
        //改写student的tostring方法,使其变成想要的格式
        for (student student : a1) {
            bw.write(student.toString());
            bw.newLine();
        }
        bw.close();



    }
}


package dianamingq;

public class student {
    String name;
    String gender;
    int age;
    double weight;

    public student() {
    }

    public student(String name, String gender, String age, String weight) {
        this.name = name;
        this.gender = gender;
        this.age = Integer.parseInt(age);
        this.weight = Double.parseDouble(weight);
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * 设置
     * @param gender
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return weight
     */
    public double getWeight() {
        return weight;
    }

    /**
     * 设置
     * @param weight
     */
    public void setWeight(double weight) {
        this.weight = weight;
    }

    public String toString() {
        return name+"-"+gender+"-"+age+"-"+weight;
    }
}

在C#中,权重随机选择通常涉及到概率分布和随机数生成。一个常见的方法是使用`System.Collections.Generic.Dictionary<TKey, TValue>`或者`Dictionary<T, double>`来存储元素及其对应的权重,然后利用这些权重来计算每个元素被选中的概率。这里有一个简单的概念实现: ```csharp using System; using System.Collections.Generic; using System.Linq; class WeightedRandomSelection { private readonly Dictionary<int, double> weights; public WeightedRandomSelection(Dictionary<int, double> weights) { this.weights = weights; // 确保所有权重的和为1(或接近于1),以便进行正确的概率计算 double totalWeight = weights.Values.Sum(); if (totalWeight != 1) { weights = weights.ToDictionary(kvp => kvp.Key, kvp => kvp.Value / totalWeight); } } public int Choose() { double randomValue = Random.NextDouble(); // 0到1之间的随机浮点数 double cumulativeProbability = 0; foreach (var (key, weight) in weights) { cumulativeProbability += weight; if (randomValue <= cumulativeProbability) { return key; } } throw new InvalidOperationException("无法生成随机选择,可能是因为权重总和不为1"); } } public class Program { static void Main(string[] args) { Dictionary<int, double> selections = new Dictionary<int, double> { {1, 0.3}, {2, 0.4}, {3, 0.3} }; WeightedRandomSelection random = new WeightedRandomSelection(selections); int chosenNumber = random.Choose(); Console.WriteLine($"随机选择了数字: {chosenNumber}"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值