• 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: Jeremy Pereira <email@hidden>
  • Date: Thu, 6 Aug 2009 17:12:59 +0100

That's a bit convoluted.  How about:

srandom(time(NULL));
array = [[NSMutableArray alloc] init];
while ([array count] < 15)
{
	int randomNo = random() % 15 + 1;	// Gives biased results, by the way
	NSNumber numberToAdd = [NSNumber numberWithInt: randomNo];
	if (![array containsObject: numberToAdd])
	{
		[array addObject: numberToAdd];
	}
}

(Written in mail and not compiled or tested).

On 6 Aug 2009, at 13:08, Mahaboob wrote:

Yeah, you are right.
I did my own code and is working well for me.
My code is :

  srandom(time(NULL));
  BOOL val;
  val = FALSE;
  array = [[NSMutableArray alloc]initWithCapacity:15];
  firstNo = random()+1;
  [array addObject:[NSNumber numberWithInt:firstNo]];
  for(i=1;i<15;i++){
      firstNo = random()+1;
      for(j=0;j<[array count];j++){

          if((firstNo == [[array objectAtIndex:j]intValue] )){
              i--;
              val = TRUE;
              break;
          }

      }
      if(val){
          val = FALSE;
          continue;
      }
      else{

          [array addObject:[NSNumber numberWithInt:firstNo]];

      }
  }


Thanks


On 8/6/09 1:50 PM, "Alastair Houghton" <alastair@alastairs- place.net> wrote:


On 6 Aug 2009, at 05:59, Mahaboob wrote:

Thanks.
It is working well.

I'll say again, off-list, you probably don't want to use Agha Khan's code; it generates biased results, which are bad in almost all applications. Take a look at the Wikipedia article I pointed you at, here, for (a) an algorithm that works, and (b) some notes on generating random numbers:

 <http://en.wikipedia.org/wiki/Knuth_shuffle>

Kind regards,

Alastair.

--
http://alastairs-place.net





_______________________________________________

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

_______________________________________________

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


  • Follow-Ups:
    • Re: Generating random numbers
      • From: Mac First <email@hidden>
References: 
 >Re: Generating random numbers (From: Mahaboob <email@hidden>)

  • Prev by Date: Re: [iPhone] networking
  • Next by Date: Re: Generating random numbers
  • Previous by thread: Re: Generating random numbers
  • Next by thread: Re: Generating random numbers
  • Index(es):
    • Date
    • Thread