Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7649351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:56:36+00:00 2026-05-31T10:56:36+00:00

I use xcode 4 on Mac Os X snow leopard. I am trying to

  • 0

I use xcode 4 on Mac Os X snow leopard.
I am trying to write a function to generate a random number within an interval, except some numbers.
ie:

int num=5;
random(time(NULL),1,100,&num,NULL);

This must generate a number within 1 and 100, except 5.
So I’ve written this code:

//
//  main.cpp
//  CPPTest
//
//  Created by Ramy Al Zuhouri on 3/7/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <exception>
#include <cstdarg>
#include <cstdlib>
#include <ctime>

using namespace std;

class cannotGenerate : public exception
{
    virtual const char* what()
    {
        return "Cannot generate any number, maybe the interval is empty, or the exluding vector is too large\n";
    }
};

// Random number generation
// min : min number (included)
// max : max number (included)
// seed : generator seed
// excp1 first argument of int pointers (the last must be NULL)
// the int pointers represent the numbers that must be excluded
// so that if such number is generated, it has to be generated again

int random(unsigned int seed=(unsigned int)time(NULL),int min=0,int max=RAND_MAX, int* excp1=NULL , ...)
{
    // variables

    int result,*ptr;
    va_list vl;
    static unsigned int count=0;

    // instructions

    srand(seed+count++);
    va_start(vl,excp1);
    do   // this do-while has the purpose to keep generating numbers until the number is correct
    {    // correct = within the [min,max] interval and not a number of the list of pointers
        result=rand()%(max-min+1)+min;
        ptr=excp1;
        do  // this do-while has the purpose to check all the va_list and to read all arguments   
        {   // passed to the function
            if(ptr!=NULL)
            {                     // here there is the exception (with gdb 6.3.50) :
                if(result==*ptr)  // Thread 1: Program received signal: "EXC_BAD_ACCESS". 
                    result=min-1;
                ptr=va_arg(vl,int*);
            }
            else
            {
                break;
            }
        }while(ptr!=NULL && (result<min || result>min));
    }while(result<min || result>max);
    va_end(vl);

    // result

    return result;
}

int main (int argc, const char * argv[])
{
    int excp[]={1,2,3,4};
    int rand;
    for(int i=0;i<100;i++)
    {
        rand=random((unsigned int)time(NULL),1,100,&excp[0],&excp[1],&excp[2],&excp[3],NULL);
        if(rand==1 || rand==2 || rand==3 || rand==4)
        {
            cerr << "error" << endl;
            break;
        }
        else
        {
            printf("%d\n",rand);
        }
    }
    return 0;
}

But with gdb I’m getting this exception:
Thread 1: Program received signal: “EXC_BAD_ACCESS”.
In that block ptr can’t be NULL, so I can’t figure out the reason of this exception.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:56:37+00:00Added an answer on May 31, 2026 at 10:56 am

    The issue is that you don’t reset the variable argument list after the first run through so it ends up pointing to the memory after the NULL input which results in the exception (if you’re lucky).

    Try replacing the inner loop in random with the following (note placement of va_start and va_end).

     do {
            result=rand()%(max-min+1)+min;
            va_start(vl,excp1);           //note
            ptr=excp1;
    
            do 
            {
                if(ptr!=NULL)
                {
                    if(result==*ptr)
                        result=min-1;
                    ptr=va_arg(vl,int*);
                }
                else
                {
                    break;
                }
            } while(ptr!=NULL && (result<min || result>min));
    
            va_end(vl); //note
        }while(result<min || result>max);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use CMake to generate an Xcode configuration for the iPhone by
I am trying to compile Geekinfo on Mac OS X 10.6.6 Snow Leopard. My
I've been trying to use Xcode 4 on my 1GB Mac Mini. It's not
I use Zend Server CE on my Mac running OSX Snow Leopard for my
I use MacVim as my editor within Xcode. By default Command-B and :make are
In an Xcode project, if I use std::cout to write to the console the
I am on a Mac with Snow Leopard (10.6.3). I hear that the assembly
I am trying to use Xcode 3.2 for java tool development. My project has
I am migrating to OS X and now trying to use Xcode. There is
I am trying to install OpenCV on my Mac (OS X v10.6.3) for use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.