0

Hi, in one of my applications I have to assign different different sections and in each section I have few imageviews.Ii have to launch a popover on any imageview for that I need that image tag value. Those images tag values I have to distinguise for that I used one varialble called count it will start with zero. It will increment based on number of sections and assign the tag values to the imageviews as

NSString *imageTagStr = [[NSString alloc]initWithFormat:@"%d%d",count,j-1];
image1.tag = [imageTagStr intvalue];

In this I am assigning the tagvalue to the imageviews and all these imageviews are created programatically using for loop.

Next thing is I have to longpress on any image which is available in any section popover have to come and once the pop over launch I have to select the button available on popover then another view will come.

On that view I will load data that I am getting from array. Here based on imagetag value means substringfromindex:1 by using this I am getting the data available in array.

Now the issue is if number of sections are more than 9 then count will become 10 so if I use substringfromindex means I am getting (like 121 in this tag value is 1 and count is 12 so if I fetch the object from array which is available in 21 means I am getting the wrong data so I have to fix this.)

So to resolve this I feel generating random two digit numbers is good and that number I have to add infornt of image tag value. Then always this will be two digits, but i dont know how to generate non repeated two digit numbers. Please if anyone have any idea pls share with me.

If is there any sample code pls share with me. thanks a lot.

Paul Hunter
  • 4,453
  • 3
  • 25
  • 31
Naresh
  • 19
  • 2

5 Answers5

2
int intRanIndex = arc4random() % 100;

Note :- It will return a random no. between 0 to 99.....

0
int min=1;
int max=16;

int randNum = rand() % (max - min) + min;
NSLog(@"random number %i",randNum);
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • hi sorry actually my requirement is changed now. means i have different sections for each section i have a data and that data is available in different arrys. all those arrays are in one array. like section1array,section2array,section3array. – Naresh Sep 06 '12 at 04:53
  • allarrays is having all the above arrays. now after a long press on imageview based on first charater of the tag value i will fetch that section array from all arrays and will display on view. now my issues is for getting that first character from tag value of imageview i am using substringtoindex:1 . this is useful upto 9 after that reference for my array is first two digits instead of one that time what is the comdition i have to write for getting two digits. – Naresh Sep 06 '12 at 04:53
  • And how can i frame the condition for both the conditions. i dont know on which image i will perform longpress so please help me in this issue. – Naresh Sep 06 '12 at 04:53
  • i unable to understand your problem of getting first character of tag...why are you getting first character.just get complete tag..and then use divide and reminder rule... like tagValue/10 or tagvalue%10 – Rajneesh071 Sep 06 '12 at 07:51
0

Why not just add 10 to count?

NSString *imageTagStr = [[NSString alloc]initWithFormat:@"%d%d", 10+count , j-1];
image1.tag = [imageTagStr intvalue];

If you don't want to do this, I suggest you take a look at this answer: Non repeating random numbers

Community
  • 1
  • 1
Paul Hunter
  • 4,453
  • 3
  • 25
  • 31
0

There is no need for random numbers here. Set

image1.tag = 256 * count + j;

then you can recover count and j with

count = image1.tag / 256;
j = image1.tag % 256;

This works for j < 256. If you need larger values for j, replace 256 by a larger factor.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0
int randomIndex = rand() % 10;
NSInteger unrepeated=2147483647;

NSLog(@"randomIndex :- %d",randomIndex);
NSLog(@"arrRandomValue indexOfObject:randomIndex :- %d",[arrRandomValue indexOfObject:[NSString stringWithFormat:@"%d",randomIndex]]);

if ([arrRandomValue indexOfObject:[NSString stringWithFormat:@"%d",randomIndex]]==2147483647)
{
    [arrRandomValue addObject:[NSString stringWithFormat:@"%d",randomIndex]];
    unrepeated=randomIndex;
    NSLog(@"unrepeated random no :- %d",unrepeated);
}

Note:- Unrepeated var. will contain either 2147483647 or unrepeated 0 to 9 random value

OR

int randomIndex = rand() % 10;
NSInteger unRepeted=2147483647;

NSLog(@"randomIndex :- %d",randomIndex);
NSLog(@"arrRandomValue indexOfObject:randomIndex :- %d",[arrRandomValue indexOfObject:[NSNumber numberWithInt:randomIndex]]);

if ([arrRandomValue indexOfObject:[NSNumber numberWithInt:randomIndex]]==2147483647)
{
    [arrRandomValue addObject:[NSNumber numberWithInt:randomIndex]];
    unRepeted=randomIndex;
    NSLog(@"un repeted random no :- %d",unRepeted);
}