• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Generating random numbers
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Generating random numbers


  • Subject: Re: Generating random numbers
  • From: Graham Cox <email@hidden>
  • Date: Wed, 5 Aug 2009 21:16:16 +1000


On 05/08/2009, at 8:44 PM, Mahaboob wrote:

I need to produce 15 random numbers between 1 to 16 without repeating any
number. I used the code like


   int i,j;
   for(i=0;i<15;i++){
       j =random() % 15 +1;
       NSLog(@"No: %d => %d \n",i,j);
       srandom(time(NULL)+i);
   }
But some numbers are repeating.
How can I do it without repeating the numbers?


A truly random number may be repeated. If it's guaranteed not to, it's not random.

Maybe you're really just trying to randomly order a number of objects? One way to do that is to assign a random number to each object and sort the list on the random number.

--Graham


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Generating random numbers (From: Mahaboob <email@hidden>)

  • Prev by Date: Re: Tell which type is in NSValue?
  • Next by Date: Re: Main Event queue
  • Previous by thread: Re: Generating random numbers
  • Next by thread: Re: Generating random numbers
  • Index(es):
    • Date
    • Thread