开发者

Display Random words from list of 50 and increase appearance of 1 word after certain time

I have NSMutableArray named *arrWords containing list of 50 words. e.g. Bond/Bind/King/Sing/Ring etc.

I use following function to display random words on screen.

-(void)displayRandomWord //This is called from timer so it randomly shows words on screen
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(150,150,200,30)];
    [lbl setText:[arrWords objectAtIndex:round([self randomXValueBetween:0 andValue:49])];
    [self.view addSubView:[lbl]; //this label will be removed after 1 second I have written code for that.
}
- (float)rando开发者_高级运维mXValueBetween:(float)low andValue:(float)high {
    return (((float) arc4random() / 0xFFFFFFFFu) * (high - low)) + low;
}

This works fine. Now my problem is let say after certain amount of time I want to increase frequency of the particular word to be appear.

I want to setup logic like initially "Sing" is to be appeared 3 times in first 10 seconds. After 10 seconds I want it to be appear atleast 2 times more than 3 times (i.e. 5 times).

Because it is random picking of words "Sing" may appear 3 times in first 10 seconds or may not.

I am stuck here to setup logic. Any suggestions ?


My first thought was to increase the amount of word "Sing" after certain time.

What do I mean is this:

If I have 50 words. A chance to get a "sing" word is 1/50. Now after 10 seconds I insert additional word "sing" to the array. Now I have 51 words in it. My chance of getting word "sing" just increased to" 2/51.

That is the simplest way I could think of.

Hope that helps.

EDIT: I just thought that if you want to make sure that you have at least some amount. You could do it like this.

If you know exactly how many words you will display in first 10 seconds? Example. 10 which is 1 per second. Then you could do this:

Pseudocode

word = get me a random word from array;
displayCount = 0; number of words already displayed;
max = 10; Displaying 10 words per 10 second
targetWord = "Sing"
targetWordCount = 0;  number of words targeted for this time frame;
requiredTargetWordCount = 2; number of times a word should be displayed during the time frame

for(word; displayCount < max; word.next){
   if word = targetWord
       targetWordCount ++
       display word
   else if displayCount < max - requiredTargetWordCount && targetWordCount != requiredTargetWordCount
       display targetWord  
       targetWordCount ++

displayCount ++

}


You can add words weights. Declare another NSMutableArray (let's say arrWordsWeights) of the same size as arrWords. Fill it with all 1's initially. Then you can increase or decrease frequency of any word just by increasing or decreasing it's frequency in weights array. To get the random index which respects the weighting you can use the following code:

int maxVal = 0;
for (int i in arrWordsWeights)
    maxVal += i;
int r = arc4random() % maxVal + 1;
int index = 0;
while (r > 0 && index < [arrWordsWeights size])
{
    r -= [arrWordsWeights objectAtIndex:index];
    index++;
}
return index - 1;

At the moment I can't check if it will compile, but at least you should get an idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜