2014-12-15 4 views
0

난수를 얻으려고합니다 : 1, 2, 3 또는 4하지만 어떻게 동일한 숫자를 두 번 연속으로 반환 할 수 있습니까?1에서 4 사이의 mt_rand하지만 연속으로 두 번 이상 동일하지 않음

function largeplate() { 
    $('.hero.largeplate').css({"background-image":"url(<?php echo get_bloginfo('template_directory');?>/images/hero/largeplate-" + mt_rand(1, 4) + ".png)"}); 
}; 
function mediumplate() { 
    $('.hero.mediumplate').css({"background-image":"url(<?php echo get_bloginfo('template_directory');?>/images/hero/mediumplate-" + mt_rand(1, 4) + ".png)"}); 
}; 

<svg version="1.1" baseProfile="basic" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1400 748" xml:space="preserve"> 
<path id="largeplate" onmouseover="largeplate();" style="opacity:0;fill:#FFFFFF;" d="M66.785,745.854c-4.209-21.595-8.774-52.639-9.001-90.009 c-1.378-226.627,158.584-388.207,202.253-429.179c46.677-43.793,191.583-166.611,414-176c46.574-1.966,241.48-5.076,420,134.667 c166.267,130.151,234.259,318.415,244,466c2.623,39.737,0.779,72.842-1.333,95.333L66.785,745.854z"/> 
<path id="mediumplate" onmouseover="mediumplate();" style="opacity:0;fill:#FFFFFF;" d="M124.704,746.667c-1.252-7.876-2.831-19.121-4-32.667 c-13.967-161.797,61.447-292.434,96.667-344.667C290.895,260.294,384.142,203.909,430.038,180 c53.978-28.119,168.826-77.147,317.333-64c219.724,19.452,351.781,160.728,374.667,186c148.734,164.246,148.901,356.341,148,397.333 c-0.889,15.778-1.778,31.556-2.667,47.333H124.704z"/> 
</svg> 

나는 이미지를 오버레이이 투명 SVG, 당신이 섹션의 마우스를 할 때마다 만든 함수가 호출 및 변경할 수있는 이미지를 트리거하지만이 두 번 같은 번호를 반환하는 경우, 그것은 것처럼 보인다 작동하지 않습니다.

+2

테이크'범위 (1,4)의 array_rand''두 요소? (array_flip은'array_rand'가 키를 반환하기 때문에) –

+1

자바 스크립트 컨텍스트에서'mt_rand()'를 대신 호출하고있다. PHP의. –

+0

@ Ja͢ck 그는 거기에' Interrobang

답변

0

내가 전달 된 최대의 한계 내에서 임의의 정수를 생성하고 두 번 연속 같은 번호를 생성하지 않습니다이 기능으로 끝났다.

c=[]; 
function makeUniqueRandom(d){ 
    if(!c.length){ 
     for(var e=1;e<d;e++){ 
      c.push(e) 
     } 
    } 
    c.splice(Math.floor(Math.random()*c.length),b=c[e],1); 
    return b 
}; 

function randomizeBackground() { 
    $('.elementA').css({ "background-image":"url('/images/image-" + makeUniqueRandom(4) + ".png)" }); 
} 

<div class="elementA" onmouseover="randomizeBackground();"></div> 
2

이 복잡성에 대해서는 mt_rand이 필요하지 않습니다.

$plateBackgroundVariants = [1,2,3,4]; 
shuffle($plateBackgroundVariants); 
// use $plateBackgroundVariants[0] for the "large" link 
// use $plateBackgroundVariants[1] for the "medium link 

이제 그들은 결코 같지 않을 것입니다.

0

이전 결과와 다른 결과를 줄 때까지 while 루프에서 mt_rand를 통해 마지막 결과와 루프를 저장할 수 있습니다. 이것은 매우 불행한 경우 새로운 번호를 가져 오는 데 시간이 걸릴 수 있음을 의미합니다.

프로그램 종료를 보장하는 또 다른 솔루션은 이전 결과를 저장하고 이전 값없이 목록을 생성 한 다음 예를 들어 array_rand를 사용하여이 목록에서 임의로 숫자를 가져 오는 것입니다. 그러나 그것은 무작위 가능성의 작은 숫자가 있다고 가정합니다.

+0

크 누스 셔플은 여기에서 잘 작동합니다. –

0

당신은 이전 값을 추적하기 위해 static 변수를 사용하여 mt_rand() 래퍼를 작성할 수 :이 기능은 $min === $max 경우 무한 루프를 입력합니다

function my_rand ($min, $max) 
{ 
    static $prev = NULL; 

    do { 
    $r = mt_rand ($min, $max); 
    } while ($r === $prev); 

    $prev = $r; 

    return $r; 
} 

하는 것으로. 이것을 막을 수 있습니다. 예 :

function my_rand ($min, $max) 
{ 
    if ($min === $max) 
    return $min; 
    ... 
+0

도움이 된 고맙습니다. – Jim

0

아카이브하려면 마지막 값을 어딘가에 저장해야하므로 다시 사용하지 않아야합니다.

운이 좋게 계산하지 않으려면 마지막 숫자가있는 경우 $ max를 1로 줄여야하므로 마지막 번호는 건너 뛰고 항상 한 번에 새 번호가 표시됩니다. 이 같은

뭔가 :

function example_rand($previous = null) 
{ 
    if($previous === null) 
     $number = mt_rand(1, 4); 
    else 
    { 
     $number = mt_rand(1, 3); 
     if($number >= $previous) 
      $number++; 
    } 

    return $number; 
}