Generate random number from given number

可紊 提交于 2019-12-23 18:14:42

问题


I am stuck in one issue regarding to random number. I am generating random number using arc4random(). I want to generate randome numbers between 0-9 at first. so i am using:

int RandomNumber = arc4random() % 9;

its good. Now suppose I got 8. I don't want my new random number to be 8. So I have done following:

NSMutableArray *aryTemp = [NSMutableArray new];
for (int i = 0; i < [aryTemp Count] < 9; i++) {
   int RandomNumber;
   do {
      RandomNumber = arc4random() % 9;
   }
   while ([aryTemp containsObject:[NSNumber numberWithInt:RandomNumber]])
   [aryTemp addObject:[NSNumber numberWithInt:RandomNumber]];
}

So I am generating a random number than adding it to array than checking for the next random number that is this the previous generated random number or not? If not than add it to the array.

It works fine for few starting numbers but when I left of with few option at that time I have very less chances to generate desire random number and while loop processes takes lots of time due to redundant random number.

What I am looking for I want random numbers of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 If I got 6 at first time next time the random number must be with in these numbers only 0, 1, 2, 3, 4, 5, 7, 8, 9


回答1:


Edit: The answer linked in the comments above proposes a more elegant solution - shuffling the array in place, although it uses the same principal as my naive approach :)

If I understand you correctly: You are wanting to select all items from the range 0-9 at random.

My initial approach would be to create a bag with all the items in the range. Each time you randomly select an item - remove it from the bag so you don't select it again.

The following code snippet should achieve this:

int randomNumber;

// Fill remainingNumbers array with numbers 0-9
NSMutableArray *remainingNumbers = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
    [remainingNumbers addObject:[NSNumber numberWithInt:i]];
}

// Create tempNumbers array to store our random numbers in
NSMutableArray *tempNumbers = [NSMutableArray array];


for (int i = 0; i < 9; i++) {
    // Create a random number in the range of the indexs for remainingNumbers array
    int maxIndex = [remainingNumbers count] -1;
    randomNumber = arc4random() % maxIndex;

    // Add random number to tempNumbers array
    [tempNumbers addObject:[remainingNumbers objectAtIndex:randomNumber]];

    // Remove the number from the remaining numbers so we don't add duplicates
    [remainingNumbers removeObjectAtIndex:randomNumber];
}
// Add final number
[tempNumbers addObject:[remainingNumbers objectAtIndex:0]];


// Print the array of 'randomly selected numbers'
NSLog(@"The array is: %@", tempNumbers);



回答2:


try this one

//call method 
int tempvalue = (int) [self randomFloatBetween:0 secondParam:9]; // pass number as per your need


-(float) randomFloatBetween:(int) smallNumber secondParam:(float) bigNumber
{
    float diff = bigNumber - smallNumber;
    return (((float) rand() / RAND_MAX) * diff)
    + smallNumber;
}

Hope this helps, thanks.

Thank you. Happy coding



来源:https://stackoverflow.com/questions/15680582/generate-random-number-from-given-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!