2011-11-21 2 views
1

나는이 방법이 있습니다무작위 부동을위한 수학 방정식?

- (float)randomFloatBetween:(float)num1 andLargerFloat:(float)num2 { 
return ((float)arc4random()/ARC4RANDOM_MAX) * num2-num1 + num1; 
} 

을 내가 대신 다음에 대한 조건문을 사용하여 가능하면 것을 궁금 :

When the score is: 
Score 0-20: I want a float between 4.0-4.5 using the above method 
Score 21-40: I want a float between 3.0-3.5 using the above method 
Score 41-60: I want a float between 2.5-3.0 using the above method 
Score 61+: I want a float between 2.0-2.5 using the above method 
: 나는이 같은 내 게임에 대한 임의의 부동 소수점을 만들고 싶어

이제 조건부를 사용할 수는 있지만이를 수행하는 것보다 쉬운 수학 공식이 있습니까?

감사합니다.

EDIT1 :

- (float)determineFloat { 
    if (score <= 60) 
    { 
     //Gets the tens place digit, asserting >= 0. 
     int f = fmax(floor((score - 1)/10), 0); 

     switch (f) 
     { 
      case 0: 
      case 1: 
      { 
       // return float between 4.0 and 4.5 
       [self randomFloatBetween:4.0 andLargerFloat:4.5]; 
      } 
      case 2: 
      case 3: 
      { 
       // return float between 3.0 and 3.5 
       [self randomFloatBetween:3 andLargerFloat:3.5]; 
      } 
      case 4: 
      case 5: 
      { 
       // return float between 2.5 and 3.0 
       [self randomFloatBetween:2.5 andLargerFloat:3]; 
      } 
      default: 
      { 
       return 0; 
      } 
     } 
    } 
    else 
    { 
     // return float between 2.0 and 2.5 
     [self randomFloatBetween:2.0 andLargerFloat:2.5]; 
    } 
    return; 
} 

괜찮나이? 또한 이것이 가장 효율적인 방법이라고 확신합니까?

답변

2

아마도 관계가 연속적이지 않기 때문에 아마 그렇지 않을 것입니다. 이런 종류의 요구 사항이있을 때는 조건문이나 switch 문을 사용하는 것이 좋습니다. 사용자와 코드를 읽거나 디버깅하는 사람은 함수가 수행하는 작업을 정확히 알 수 있습니다. 이 경우 어떤 종류의 수학 함수를 사용하면 매우 복잡 할 수 있습니다. 프로세스를 느리게 진행할 가능성이 큽니다. 스위치를 사용

가능성 :

-(float)determineFloat:(float)score 
{ 
    if (score <= 60) 
    { 
     //Gets the tens place digit, asserting >= 0. 
     int f = (int)fmax(floor((score - 1)/10.0f), 0); 

     switch (f) 
     { 
      case 0: 
      case 1: 
      { 
       return [self randomFloatBetween:4.0 andLargerFloat:4.5]; 
      } 
      case 2: 
      case 3: 
      { 
       return [self randomFloatBetween:3.0 andLargerFloat:3.5]; 
      } 
      case 4: 
      case 5: 
      { 
       return [self randomFloatBetween:2.5 andLargerFloat:3.0]; 
      } 
      default: 
      { 
       return 0; 
      } 
     } 
    } 
    else 
    { 
     return [self randomFloatBetween:2.0 andLargerFloat:2.5]; 
    } 
} 

용도 :

float myScore = 33; 
float randomFloat = [self determineFloat:myScore]; 

지금, randomFloat 3과 3.5 사이의 값이 될 것이다.

+0

조건부를 사용할 때 가장 효율적인 방법은 무엇입니까? if-> else if-> else if, 등 ...? –

+0

최대 점수가 있습니까? –

+0

아니요 최대 점수가 없습니다 –