Леший Posted July 15, 2010 Report Share Posted July 15, 2010 (edited) Ir veseli sklaitļi no A līdz B, ir skaitļis N, vajag saģenerēt masīvu ar N elementiem, kuri būtu randoma paņemtie integeri no A līdz B, bet tā, lai masīvs būtu puslīdz līdzsvarots, tas ir, lai katrs skaitlis parādītos puslīdz vienādi. Piemēram, A = 1, B = 3, N = 10 2, 1, 2, 3, 1, 2, 1, 1, 3, 3 šitāds der. 3, 1, 3, 3, 3, 3, 3, 2, 3, 3 šitāds neder. Tiku līdz masīva ģenerācijai un shuffle, bet tad masīvs sanāk pārāk līdzsvarots, man vajag nedaudz randomizētāku. Edited July 15, 2010 by Леший Quote Link to comment Share on other sites More sharing options...
Grey_Wolf Posted July 15, 2010 Report Share Posted July 15, 2010 (edited) Tiku līdz masīva ģenerācijai un shuffle, bet tad masīvs sanāk pārāk līdzsvarots, man vajag nedaudz randomizētāku. aaa?? Kas mt_rand() neder?? Tur manuali shadi skriptinji ka biezss... izvelies piemerotako .. vienkarshi var parbaudit vai 2 liidz n ieprieksejie netkartojas .. tad arii buus gan randomiski, gan daudzmaz lidzsvaroti Edited July 15, 2010 by Grey_Wolf Quote Link to comment Share on other sites More sharing options...
Леший Posted July 15, 2010 Author Report Share Posted July 15, 2010 Paštukoju, sanāca šitāds: function generateArray($N, $B, $A = 1){ $return = array(); $avg = (int)$N/($B-$A); $sqrt = (int)sqrt($N)/($B-$A); for ($i = $A; $i <= $B; $i++){ $cnt = $avg + rand(-$sqrt, $sqrt); if ($cnt > 0) { for ($k=0; $k<$cnt;$k++){ $return[] = $i; } } } shuffle($return); return array_slice($return, 0, $N); } Quote Link to comment Share on other sites More sharing options...
ohmygod Posted July 15, 2010 Report Share Posted July 15, 2010 Neder uzģenerēt masīvu ar vajadzīgo skaitu katra elementa un tad vienkārši randomizēt masīvu??? Quote Link to comment Share on other sites More sharing options...
codez Posted July 15, 2010 Report Share Posted July 15, 2010 (edited) function generateArray($n, $b, $a = 1){ $arr=$r=range($a,$b); while (count($arr=array_merge($arr,$r))<$n); shuffle($arr); return array_slice($arr, 0, $n); } Edited July 15, 2010 by codez Quote Link to comment Share on other sites More sharing options...
Леший Posted July 15, 2010 Author Report Share Posted July 15, 2010 codez, tnx, bet, ja N būs krietni lielāks, ka B-A, tad tavs shuffle ciklā laikam būs nāvējošs. Quote Link to comment Share on other sites More sharing options...
codez Posted July 15, 2010 Report Share Posted July 15, 2010 (edited) yep, aiz while vajag semikolu. P.S. Lai nejauktos prāts, prātīgāk būtu rakstīt: function generateArray($n, $b, $a = 1){ $arr=$r=range($a,$b); while (count($arr)<$n){ $arr=array_merge($arr,$r); } shuffle($arr); return array_slice($arr, 0, $n); } Edited July 15, 2010 by codez Quote Link to comment Share on other sites More sharing options...
mefisto Posted July 15, 2010 Report Share Posted July 15, 2010 (edited) Леший , ka ko tu tur mudies. Tev tak vajag tikai pārbaudīt, lai ģenerētajā randomā neatkārtojas 3 vienādi skaitļi pēc kārtas .. vai tad ne ? edit: sry, Grey_Wolf jau šito pašu uzrakstīja =/ Edited July 15, 2010 by mefisto Quote Link to comment Share on other sites More sharing options...
Grey_Wolf Posted July 16, 2010 Report Share Posted July 16, 2010 uz fikso kautkaa shadi <pre> <?php $i=0; while($i<10) { if(!isset($masivs)) { $masivs[]=mt_rand(1,500); // izveidojam masivu un generejam pirmo elementu $i++; // palielinam skaititaju par 1 } else { // parbaudam vai dotais X jau neeksiste masivaa $randoms=mt_rand(1,500); if($masivs[($i-1)]!=$randoms AND $masivs[($i-2)]!=$randoms AND $masivs[($i-3)]!=$randoms ) { $masivs[]=mt_rand(1,500); // ierakstam randomaizo elementu masiivaa $i++; // palielinam skaititaju par 1 } } } print_r($masivs); ?> nush funkcijaa shamo sabaziisi pats.. P.S. masiva randomiska kartosana ir laba, ja piemeram vajag samaisiit spelju kartis (ir zinams skaits , tikjasamaisa) Quote Link to comment Share on other sites More sharing options...
codez Posted July 16, 2010 Report Share Posted July 16, 2010 mefistu un gery_wolf, jūsu piedāvātie varianti var radīt arī krietni nesabalncētus masīvus, piemēram, ja A=1 un B=9, tad jūsu piedāvātie varianti var uzģenerēt šādu masīvu: 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 kamēr sabalnsēts masīvs, būt, piemēram: 1 9 3 6 7 2 4 8 5 7 3 2 8 5 9 4 1 4 9 2 Quote Link to comment Share on other sites More sharing options...
Grey_Wolf Posted July 16, 2010 Report Share Posted July 16, 2010 jūsu piedāvātie varianti var radīt arī krietni nesabalncētus masīvus randoms peec savas butibas tachu ir gadijuma skaitlis, attiecigi Metamo kaulinju teoretiski arii var uzmest 10 reizes seshinieku ... ja nepieciesams lai Neatkartotos tad jparbauda mazliet savadak .. piedevam mt_rand() shamo njem vera un faktiski skaitlji neatkartojas (neliela ieksejaa parbaude) Quote Link to comment Share on other sites More sharing options...
Леший Posted July 16, 2010 Author Report Share Posted July 16, 2010 Grey_Wolf, kā teica Robert R. Coveyou The generation of random numbers is too important to be left to chance. Quote Link to comment Share on other sites More sharing options...
mefisto Posted July 16, 2010 Report Share Posted July 16, 2010 Nu kaut kā šitā ... sanāk +/- balancēts /* * Returns a semi-balanced array * * @param {int} [length] The length of generated array ( default = 10 ) * @param {int} [lower] Lowest possible value in array * @param {int} [higher] Highest value in generated array * * @return {array} The generated array * */ function array_rnd_balancer( $length = 10, $lower = 0 , $higher = 10){ // the scale of adjustment .. // where 0 is no ajustment and #INF# - constant adjustment $coefficient = sqrt( $higher - $lower ); // result set $data = array(); // temporary range values $l = $lower; $h = $higher; // holds the amount of adjustment for each step $dh = $dl = 0; // holds the value of previous array element // to prevent repeating two in a row $previous = $lower - 1; // initializes the $total = $average = round( ( $h - $l ) / 2 ) ; for ( $i = 1; $i <= $length; $i++ ){ //adjust the bounds of possible values if ( $total >= $average * $i ){ $dh += $coefficient / ( $coefficient + $dh); $dl = 0; $h = $higher - round( $dh ); $l = $lower; }else{ $dh = 0; $dl += $coefficient / ( $coefficient + $dl); $h = $higher; $l = $lower + round( $dl ); } //makes sure that there ain't two same values in a row $current = rand( $l , $h ); while ( $current === $previous ){ $current = rand( $l , $h ); } // when non-repeating value found, it becomes previous value $previous = $current; $total += $current; $data[] = $current; } return $data; } Quote Link to comment Share on other sites More sharing options...
Grey_Wolf Posted July 16, 2010 Report Share Posted July 16, 2010 (edited) Леший -> Bet tapec jau sava piemera ieliku kontroli ;) tb. nekad nesanaks vienadi ieprieksejie 3 skaitlji .. pielabo to manu skriptu, un buus jau ok. P.S. nu nav gadijumu skaitlju generacijas problema pilnibaa atrisinata.. (tiri matimatiski ) Edit: gribeju piebilst ka Matematiski NAV iespejams gadijuma skaitlis... vienmenr shamo vares izrekjinat ... Edit: hmmm nu ir tomer iespeja ;) Edited July 16, 2010 by Grey_Wolf Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.