Yesterday my husband gave me a demand: give two timestamps, randomly return a time in this time period, but there is a requirement, the probability between 9:00-23:00 is 80%,23:00-9:00 between the probability is 20%.
It sounds pretty simple ... But think very well!!!
I follow the general idea, is to get a random number from a number, some of the number of the probability is high, some numbers of the probability of small ...
First, from 1 and 2, the probability of returning 1 is 0.8, the probability of returning 2 starts with 0.2, the 0-1 axis is divided into 80% and 20%, and if it falls in the previous paragraph, it returns 1 and falls in the latter section, returning 2.
var rnum = math.random (); if (rnum<0.8) { return 1 } else { return 2 }
Follow this idea, is to start and end in the position on the axis, change the original one by one corresponding relationship, according to the proportion I want to correspond, through a calculation, I wrote a-b two number of random number, return a,a+2,a+4 ... the probability is 20%, return a+1,a+3,a+ 5 ... the probability is 80%, such a function ...
functionGetNumber (A, b) {varR = Math.random () * (b-a); //0-(b-a) varMini = R-Math.floor (R); varBig =Math.floor (R); if((big%2==0 && mini<0.4) | | (big%2==1) ) {R=Math.floor (r)}Else{R=Math.ceil (r)}if((R+a)%2==0) {Shuang+=1; } Else if((R+a)%2!==0) {Dan+=1 } returnShuang/dan;}varDan = 0;varShuang = 0; for(vari=0; i<100; i++) {Console.log (GetNumber (62,10000001))}
To get to this point, I'll consider if it corresponds to a timestamp .... The more I think the more tangled, like back to do high number of years ... At this moment, my husband told me his ideas, Sir into a time stamp, and then calculate a probability, consider whether to leave this timestamp, or regenerate ... Although not very right, but suddenly inspired me ... As a result, the following idea is immediately available:
First, the time stamp is generated in the time interval to determine if the time stamp is between 9:00-23:00, or between 23:00-9:00, if between 9:00-23:00, put into 'arr1', if between 23:00-9:00, Put it in 'arr2' until 'arr1' has 4 time data, 'arr2' has 1 time data. Then merge 'arr1' and 'arr2', Finally, a random one comes out of the merged array. It's easy! A change of mind, the problem will be solved!
/***************t1-t2 Random Rounding ***************/ functionGetrandom (t1,t2) {varR = Math.Round (Math.random () * (t2-t1)); returnr+T1; } /***************arr a random item in an array ***************/ functionGetrandomarr (arr) {varindex = Getrandom (0,arr.length-1); returnArr[index]}/***************arr a random item in an array ***************/ functionGettimearr (t1,t2) {varARR1 = []; varARR2 = []; functionPushtime () {vart =Getrandom (T1,T2); varh =NewDate (t). GetHours (); if(H>=8 && h<=22){ if(arr1.length<4) Arr1.push (t); returnJudgearr ()}Else { if(arr2.length<1) Arr2.push (t); returnJudgearr ()}} functionJudgearr () {if(Arr1.length==4 && Arr2.length==1) {Array.prototype.push.apply (ARR1,ARR2); returnarr1}Else { returnpushtime ()}} returnPushtime (); } varDate1 =NewDate (2015,0,1,0,0,0,0). GetTime (); varDate2 =NewDate (2015,1,1,0,0,0,0). GetTime (); varTime =Getrandomarr (Gettimearr (Date1,date2)); Alert (NewDate (Time). GetHours ());
There is this idea, in fact, can be more simple, for example, I first randomly generated a time, only to take its month and minute milliseconds, and the number of hours, I loop four times, each time take a random number between 9:00-23:00. And then loop again, taking only a random number between 23:00-9:00, Finally merge the array, randomly taking a time.
The calculation method of obtaining random time and unequal probability of different time periods