-3

This is my code for generating random numbers from 1-99, but it's only generating same set of numbers (15 numbers) every time. I'm storing those numbers in an NSArray and getting output in NSLog properly. It's ok, but I want different set of random numbers with no repeated number whenever I call this random method. Can any one help me please?

-(void) randoms
{

    myset=[[NSArray alloc]init];
    int D[20];
    BOOL flag;
    for (int i=0; i<15; i++)
    {
        int randum= random()%100;
        flag= true;
        int size= (sizeof D);

        for (int x=0; x<size; x++)
        {
            if (randum == D[x])
            {
                i--;
                flag= false;
                break;
            }
        }

        if (flag) D[i]=randum;

    }
    for (int j=0; j<15; j++)
        {
        myset=[myset arrayByAddingObject:[NSNumber numberWithInt:D[j]]];
        }

    NSLog(@"first set..%@",myset.description);
}
Neeku
  • 3,646
  • 8
  • 33
  • 43
ArghyaM
  • 1
  • 3
  • Give this one a try http://stackoverflow.com/questions/160890/generating-random-numbers-in-objective-c – Anupdas Jul 07 '14 at 11:20
  • That was seriously annoying. Did a nice long answer with code that has great explanation in it and this question got closed. I will add it to a blog and provide you with a link when I can. – Popeye Jul 07 '14 at 11:44
  • While there is an element that is a duplicate - generating unique random numbers, there is also the element of putting the random numbers in a set, so I have nominated to reopen – Paulw11 Jul 07 '14 at 11:49
  • 2
    @Paulw11 There are many SO questions asking this. [Take your pick](http://stackoverflow.com/search?q=%5Bc%5D+random+always+the+same) – JeremyP Jul 07 '14 at 11:54
  • @JeremyP I agree that the random seeding is a duplicate, but not the creating a set of unique random numbers within bounds. – Paulw11 Jul 07 '14 at 12:06
  • @Paulw11 Plenty of questions about [non repeating random numbers](http://stackoverflow.com/search?q=%5Bc%5D+random+non+repeating) too. – JeremyP Jul 07 '14 at 12:12
  • @Popeye: "Did a nice long answer with code that has great explanation in it and this question got closed." Why did you vote to close then? – jscs Jul 07 '14 at 18:52
  • I voted to close first then realized I was wrong. Have since voted to reopen – Popeye Jul 07 '14 at 18:55
  • It's possible to retract an already-cast vote, @Popeye, if you change your mind before the question is closed. This is a recent-ish feature. – jscs Jul 08 '14 at 17:50

3 Answers3

2

You have to seed the generator before using it. If you want to skip the seeding, you can use arc4random_uniform(). It's a different algorithm and takes care of the seeding process on its own. Other than that, you can use it in your code almost exactly like you used random(). You'll just have to specify the upper bound as a parameter, instead of using modulo:

-(void) randoms
{

    myset=[[NSArray alloc]init];
    int D[20];
    BOOL flag;
    for (int i=0; i<15; i++)
    {
        int randum= arc4random_uniform(100);
        flag= true;
        int size= (sizeof D);

        for (int x=0; x<size; x++)
        {
            if (randum == D[x])
            {
                i--;
                flag= false;
                break;
            }
        }

        if (flag) D[i]=randum;

    }
    for (int j=0; j<15; j++)
        {
        myset=[myset arrayByAddingObject:[NSNumber numberWithInt:D[j]]];
        }

    NSLog(@"first set..%@",myset.description);
}
JeremyP
  • 84,577
  • 15
  • 123
  • 161
Vingdoloras
  • 109
  • 8
0

Try this command before starting your arc4random

srand(time(NULL));
Sri
  • 827
  • 8
  • 34
  • http://stackoverflow.com/questions/8410571/non-repeating-random-numbers-in-iphone try above link... – Sri Jul 07 '14 at 11:21
  • Do you even know what that does? If so please add an explanation of what it is and does, for those that don't – Popeye Jul 07 '14 at 11:22
  • It seeds the pseudo random number generator that rand() uses.. for more detail check this link:- http://www.cplusplus.com/reference/clibrary/cstdlib/srand/ – Sri Jul 07 '14 at 11:26
0

If I understand you correctly you want a set containing 15 random numbers between 1-99. You can use the following:

- (NSSet *)randomSetOfSize:(int)size lowerBound:(int)lowerBound upperBound:(int)upperBound {
    NSMutableSet *randomSet=[NSMutableSet new];
    while (randomSet.count <size) {
        int randomInt=arc4random_uniform(upperBound-lowerBound)+lowerBound;
        NSNumber *randomNumber=[NSNumber numberWithInt:randomInt];
        [randomSet addObject:randomNumber];
    }

    return randomSet;
}

and call it with

NSSet *myRandomSet=[self randomSetOfSize:14 lowerBound:1 upperBound:99];
Paulw11
  • 108,386
  • 14
  • 159
  • 186