Question

This is for a monotouch (Xamarin Studio) iPad app

I have a survey that asks the user to look at an image and rate it, there are 140 images and they are shown randomly. I have these images in a folder and named them pic1.jpg, pic2.jpg, pic3.jpg, etc. At the start of the survey I randomly generate an array of numbers from 1-140, making sure NOT to have the same number twice (This is verified and is working....). I then go through the array and show the images in the random order.

After the update of iOS7 I am having an issues with the wrong image showing up. It will show up multiple times in one test. I have debugged this and it is simply the wrong image that is showing up, almost as if the image has been replaced by another image... For example when image 81 should have showed up image 94 actually did. This happened 12 times in the 140 images...

Here is the code for the rating:

public override void ViewDidLoad ()
{
int[] Num2 = new int[141];  //array for random numbers
int ic  = 0;  //int count
rand2(ref Num2); //routine to generate random numbers...
this.btnSEG.ValueChanged += delegate(object sender, EventArgs e){ //submit the rating
ic = ic + 1; //increase the count
imgSorce.Image.Dispose(); //clear the current image
using (var pool = new NSAutoreleasePool ()) {  //put this in to prevent leaks
this.imgSorce.Image = UIImage.FromFile ("image/pic" + Num2[ic] + ".jpg");  //display the next image
};
};

I have checked all the images in the image folder and there are not any duplicates.

Any ideas why this would be happening?

Updated

@Krumelur asked for the code to generate the random numbered array... Here it is...

private void rand2 (ref int[] rn)
{
int i = 0;
int icount = 0;
 for (i = 0; i <= 139;)
{
int n = 0;
rand3(ref n);
for(icount = 0; icount <= 139;)
{
if (n == rn[icount])
{
icount = 141;
}
icount = icount + 1;
if (icount == 140)
{
rn[i] = n;
i = i+1;
}
}
};
rn[140] = 0;
}

And here is rand3 that was referenced above...

private void rand3 (ref int num)
{
Random r = new Random();
num = r.Next(1, 141);
}

No correct solution

OTHER TIPS

Personally, I think a better solution is to create your array, populate it with sequential numbers and then shuffle the array elements. This guarantees no duplicates. Here's sample code to do that:

int[] Num2 = new int[141]; //array for random numbers
// Fill the array with numbers from 0 to 140
for (int i = 0; i < Num2.Length; i++) {
    Num2[i] = i;
}

// Shuffle Array
Random rnd = new Random((int)DateTime.Now.Ticks); // seed the random number generator based on the time so you don't get the same sequence repeatedly
for (int i = Num2.Length; i > 1; i--) {
    // Pick random element to swap
    int j = rnd.Next(1, 141);
    // Swap
    int temp = Num2[j];
    Num2[j] = Num2[i - 1];
    Num2[i-1] = temp;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top