• 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: Mahaboob <email@hidden>
  • Date: Thu, 06 Aug 2009 10:29:26 +0530
  • Thread-topic: Generating random numbers

Thanks.
It is working well.


On 8/5/09 10:24 PM, "Agha Khan" <email@hidden> wrote:

> Dear Mahaboob:
> Many people have answered this question but I will take this route.
>
> First of all you only have call srandom once in you program. Best place is
> when you are going to load the object.
>
> I defined 2 macros
>
> #define RANDOM_SEED() srandom(time(NULL))
> #define RANDOM_INT(__MIN__, __MAX__) ((__MIN__) + random() % ((__MAX__+1) -
> (__MIN__)))
>
> #define ARRAYSIZE 15
>
> Now I assume that you already called RANDOM_SEDD previously.
> Now create an array with your range of your numbers. In your case 1 to 16.
>
> int RandArray[ARRAYSIZE];
>
> for (int i = 0; i < ARRAYSIZE; i++)
> {
> RandArray[i] = i + 1; // choose your own range
> }
>
> Now shuffle them
>
> for (int i = 0; i < ARRAYSIZE; i++)
> {
> int RandomNumber = RANDOM_INT(0, ARRAYSIZE);
>       int temp = RandArray[RandomNumber];
>       RandArray[RandomNumber] = RandArray[i];
>       RandArray[i] = temp
> }
>
> :-)
> Agha
>
> On Aug 5, 2009, at 3:44 AM, 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?
>>
>> Thanks in advance
>> Mahaboob
>>
>>
>> _______________________________________________
>>
>> 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

References: 
 >Re: Generating random numbers (From: Agha Khan <email@hidden>)

  • Prev by Date: Re: NSMenuItem addTarget & retain
  • Next by Date: Re: adding a sub menu to multiple super menus?
  • Previous by thread: [OT] Re: Generating random numbers
  • Next by thread: Re: Generating random numbers
  • Index(es):
    • Date
    • Thread