-4

I have an array of 30 images. My requirement is to fetch only 20 images every time, randomly, out of that 30 images. Each and every time fetch new 20 images.

Could anyone tell me how can I achieve this?

I tried this:

randIdx=arc4random()%[FrontsCards count];

but I get all 30. How can I get 20 images from a 30 image array?

Nate
  • 31,017
  • 13
  • 83
  • 207
Jitendra
  • 5,055
  • 2
  • 22
  • 42

3 Answers3

5

Trivial approach: mutableCopy the array of images, loop from 0 to 20, select an image (idx = arc4random_uniform(copiedArray.count);) then remove the selected image from the dupe array.

(And no, don't use arc4random() % array.count, modulus makes the randomness disappear, that's why there's the arc4random_uniform() function.)

  • 1
    @JitendraDeore What more details? –  May 23 '13 at 07:42
  • i want only 20 from 30 every time new 20 images. – Jitendra May 23 '13 at 07:42
  • 1
    Maybe it would be a little more efficient to loop from 0 to 9, remove 10 images and let the remaining ones be the result. – Marcin Kuptel May 23 '13 at 07:44
  • 2
    I think Jitendra looking for some code copy paste and working. – Buntylm May 23 '13 at 07:45
  • @bit-whacker He ain't getting that. –  May 23 '13 at 07:47
  • NSMutableArray * arr=[[NSMutableArray alloc]init]; m = arc4random_uniform(arr.count); for(m=0;m<20;m++) { arr=[FrontsCards mutableCopy]; NSLog(@"%d",m); } get value serially 0,1,2,3,4; – Jitendra May 23 '13 at 07:49
  • 1
    @JitendraDeore I hope you are not serious. ***Of course it's 0, 1, 2, 3, 4...*** since you're printing the loop counter. Need to get some common sense, seriously... –  May 23 '13 at 07:51
  • how may i get random value i tried this solution but get value serially and i am serious. – Jitendra May 23 '13 at 07:53
  • 4
    @JitendraDeore `for (m = 0; m < 20; m++) { int rnd = arc4random_uniform(arr.count); NSLog(@"%d", rnd); }` but you should really get some algorithmic thought, you not getting this despite me having explained this several times indicates that you are **not** prepared for writing an iOS app. You don't even understand why the loop counter is the loop counter... this is not going to be good. –  May 23 '13 at 07:55
  • 1
    @H2CO3 Great Effort ...! :) – Buntylm May 23 '13 at 07:57
  • 3
    @H2CO3 +1 for two reasons: 1) The mention of arch4_uniform and 2) Your (relative) patience in the comments – borrrden May 23 '13 at 08:35
  • @borrrden (relative - LOL, sure!) –  May 23 '13 at 08:38
  • 1
    @H2CO3 There is one more Question(http://stackoverflow.com/questions/16712291/) on SO having almost same variable declaration and almost same issue(Random image).These people want us to code for them and they just want quick results.Where both the Questions are been posted by different users.the second user is (http://stackoverflow.com/users/2407921/chocolate).Do we need such member in our Community? – Ayush May 23 '13 at 11:48
  • @Ayush No, we don't, really. –  May 23 '13 at 11:49
  • @H2CO3 is he the same person that I think(http://stackoverflow.com/users/2407921/chocolate) because the other account was create 2 days ago. – Ayush May 23 '13 at 11:52
1
  1. shuffle the array every time before fetching
  2. fetch the top 20 images from the array.

Have a look at different shuffling algorithm.

meim
  • 740
  • 4
  • 7
0

Just use first 20 images out of all 30 and check firstly if any image is already presented in your array wouldn't be added again in the array. For this you try a loop which will run only 20 times.

iEinstein
  • 2,100
  • 1
  • 21
  • 32