序文
今、アプリは雨の後に湧き出ており、急上昇しています。経験豊富で、経験の浅い、資格があり、資格のないすべての人がビジネスを始めたいと考えています。起業家の90%以上がアプリを作成する必要があります。これは、起業家精神の標準的な構成と思われます。
アプリを作成したら、宣伝する必要があります。それを宣伝する方法は?クーポンの送信は最も不可欠なものです。現在、多くの製品または操作では、クーポンの量をランダムに発行する必要がありますが、あまりランダムにすることはできません。すべてのクーポンが送信されますか?投資家のお金ですよね?
したがって、ランダムに生成された量では、少量の確率が大きくなり、大量の確率が小さくなる必要があります。たとえば、3元の70%、5元の25%、10元の5%。この種の確率でクーポンを生成した場合はどうすればよいですか?
上記の質問では、random.next(整数範囲)を使用するだけでは不十分です。直接。この擬似ランダムは重み付けされていないため、3、5、および10の確率は同じです。
実装のアイデア
上記の例を見てみましょう。 3が表示される確率は70%です。 70の重量を割り当てます。5の出現確率は25です。重量は25です。10の確率が5%です。 5の重量を割り当てます。
重みの合計を順番に計算し、現在の数値に表示される重みの合計の前に、その重量範囲の開始点値として値を使用し、その重量範囲のエンドポイント値として合計の後の値を使用します。
このようにして、andext.next(100)を使用して乱数を作成し、乱数が落ちる範囲を判断してから、対応するクーポン値にマッピングできます。
Javaの実装
パッケージcom.nggirl.test.weight.random; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.random; public class weightrandom {public static void main(string [] args){weigtrandom wr = weightrandm string [] {"1"、 "2"、 "3"、 "4"}、new Integer [] {100,100,200,600}); random r = new Random(); for(int i = 0; i <10; i ++){integer rv = r.nextint(wr.getmaxrandomvalue()); system.out.println(rv); system.out.println(wr.getElementByRandomValue(rv).getKey() + "" + rv);} hashmap <string、integer> keycount = new Hashmap <String、 " 0); keycount.put( "2"、0); keycount.put( "3"、0); keycount.put( "4"、0); keycount.get(key).intvalue()+1);} system.out.println( "");} private list <urewedelement> weightelements; public void initweight(string [] keys、integer [] weights){if(keys == null || weights == null || keys.length.lenty = weights.lengts = weights.lents; arrayList <ueidelement>(); for(int i = 0; i <keys.length; i ++){weightelement.add(new weedelement(keys [i]、weights [i]));} rangeweightelemnts(); printrvs();} private void remints( ele0 = umeidelements.get(0); ele0.setthresholdlow(0); ele0.setthresholdhigh(ele0.getweight()); 1) (重みエレメントE:重みエレメント){if(rv> = e.getThresholdLow()&& rv <e.getThresholdhigh()){return e;}} return null;} public integer getMaxrandomValue(){if(weightelements == null || weightelements.isizize( umeidelements.get(weightelements.size() - 1).getthresholdhigh();} public void printrvs(){for(weightelement e:weightelements){system.out.println(e.tostring();}}静的クラス重量電気{/** emention ementig ** kims kike;/**/**/*******乱数の範囲低い線*/プライベート整数のしきい値;/***重量は乱数の範囲に対応しています高線*/プライベート整数のしきい値ハイ; public eedelement(){} publiceelement(integer weight){this.key = weight.tostring(); this.weight = weight;重量;} public String getKey(){return key;} public void setkey(string key){this.key = key;} public integer getweight(){return weight;} public void setweight(this.weight = weight;} public Integer getSresholow() {this.thresholdlow = thresholdlow;} public Integer getthresholdhigh(){return thresholdhigh;} public void setthresholdhigh(integer thresholdhigh){this.thresholdhigh = thresholdhigh;} public string tostring(){return "key:" + this + " +" + low: "+this.thresholdlow+" height: "+this.thresholdhigh;}}}}結果:
2 1028764 876
二分法の実装
public weightelement getElementByRandomValue(integer rv){if(rv <0 || rv> getMaxrandomValue() - 1){return null;} //重みElements.size()/2; while(true){if(rv <weightelements.get(index).getthresholdlow()){end = index -1;} else if(rv> = umeidelements.get(index).getthreshigh(){start = index + 1;} return weightelements.get(in firt)重量のランダムアルゴリズムの理解を強化するために、以下の別の例を共有し、一度に配置しましょう!
重量ランダムアルゴリズムは、宝くじ、リソーススケジューリングなどのシステムで広く使用されています。これは、重量に応じた単純なランダム実装です。重みは、いくつかのランダムオブジェクトのヒットの比率です(分類)。重量設定が高いほど、ヒットが容易になり、ヒットの合計が100に等しくなりません。
単純な実装コードは次のとおりです。
java.util.arraylist; Import java.util.list; Import java.util.random; public list <weightcategory> categories = new Arraylist <eightcategory>(); private static random = new random = new random(); public static void initdata(){weightciccatic wc1 = = new = new = New randual = new random = new random = new random = new randiou WeightCategory( "a"、60); weightcategory wc2 = new weightcategory( "b"、20); weightcategory wc3 = new weightcategory( "c"、20); categorys.add(wc1); categorys.add(wc2); categorys.add(wc3);} intic void main(); 0;(weightcategory wc:categories){weightsum + = wc.getweight();} if(weightsum <= 0){system.err.println( "error:weightsum =" + weightsum.tostring();} integer n = random.nextint(weightum);/ n/ n/ n/ n/ wc:categories){if(m <= n && n <m +wc.getweight()){system.out.println( "このランダムカテゴリは" +wc.getcategory()) WeightCategory(String Category、Integer Weight){super(); this.setcategory(category); this.setweight(weight);} public Integer getweight(){return weight;} public void setweight(integer weight){this.weight = weight;} public String getCategoryカテゴリ;}}結果:
要約します
上記は、Java言語重量ランダムアルゴリズムの実装に関するこの記事の完全な内容です。私はそれが誰にでも役立つことを願っています。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!