while condition help

This is a discussion on while condition help within the C Programming forums, part of the General Programming Boards category; Here is a snippet of code that I'm trying to generate random numbers. I only want to return when the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    5

    while condition help

    Here is a snippet of code that I'm trying to generate random numbers. I only want to return when the random number is divisible evenly by 2 or 3:
    Code:
    int genRandNum (){
    int z = 1, min = 0, max = 1000; //sets local variables for random number generation
    printf("Generating random number\n"); //test line
    srand ( time (NULL) ); //seeds random number generator do{ //loop to generate number z = (min + ((rand() * (1.0 / (RAND_MAX))) * (max - min))); //found calc on internet printf("generated: %d\n",z); //testing line }while ((z%2) == 0 || (z%3) == 0); //making sure random number fits criteria printf ("returned value = %d\n",z); //testing line return z; //returns valid value }
    Here is the output from the latest run:

    Generating random number
    generated: 56
    generated: 108
    generated: 919
    returned value = 919


    I would appreciate any constructive feedback on why it didn't stop and return the 58 OR the 108.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Code:
           }while ((z%2) == 0 || (z%3) == 0);
    This condition is wrong. You want to negate them:
    Code:
          } while (z%2 != 0 || z%3 != 0);
    Last edited by nonpuz; 12-08-2012 at 03:41 PM. Reason: Sorry you want to || not AND misread your first post

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,118
    Quote Originally Posted by Kent Luttrell View Post
    I only want to return when the random number is divisible evenly by 2 or 3:
    There is no need to change your condition, however you need to retranslate your English to C-compatible English:
    "Keep generating random numbers, but stop as soon as the generated number is divisible by 2 or 3:"

    Code:
    for(;;){  // keep generating random numbers until I say stop
        z =  (min + ((rand() * (1.0 / (RAND_MAX))) * (max - min)));
        printf("generated: %d\n",z);  
        if ((z%2) == 0 || (z%3) == 0)  //z is divisible by 2 or 3, so stop
            break;
    }

  4. #4
    Registered User
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    5
    @nonpuz:: That fix did what I wanted it to do, thank you so much. Although, now it is always generating the same number or set of numbers. I have used the time-random seed to try to avoid this. Any more input would be greatly appreciated.

    @c99tutorial:: I don't want to recode my "previously working" method. This is an assignment and I don't care to lose points doing code that I obviously haven't encountered. Now if you have any input on the seeding issue, that would be appreciated.
    Last edited by Kent Luttrell; 12-08-2012 at 04:04 PM. Reason: directed replies

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,118
    You are seeding with srand(time(NULL)) which will work "okay". For a "better" seeding you might want to include <sys/time.h> and then do something like this:

    Code:
    struct timeval t1;
    gettimeofday(&t1, NULL);
    srand(t1.tv_usec * t1.tv_sec);
    Full explanation on this page:
    Seeding srand() by Guy Rutenberg

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    With another alteration you can fix the duplicate issue here by not calling srand() every time you generate 1 random number. You should really
    only seed once per run unless you are a daemon that is up for days on end or something like that. Upon reloading config for example could be another good time.

    If instead you use a static variable to determine whether or not rand has been seeded then you only do it the first time you call genRandNum and
    get much better output:
    Code:
    int genRandNum(void)
    {
        static enum { OFF, ON } init;
        int z = 1, min = 0, max = 1000;                //sets local variables for random number generation
        printf("Generating random number\n"); //test line
    
        if (init != ON) {
            srand ( time (NULL) );                    //seeds random number generator
            init = ON;
        }
    ...
    ...
    Output:
    Code:
    Generating random number
    generated: 641
    generated: 901
    generated: 167
    generated: 75
    generated: 128
    generated: 830
    generated: 734
    generated: 178
    generated: 965
    generated: 889
    generated: 12
    returned value = 12
    Generating random number
    generated: 383
    generated: 861
    generated: 723
    generated: 821
    generated: 77
    generated: 67
    generated: 829
    generated: 917
    generated: 798
    returned value = 798
    Generating random number
    generated: 890
    generated: 881
    generated: 26
    generated: 208
    generated: 752
    generated: 359
    generated: 582
    returned value = 582
    Generating random number
    generated: 760
    generated: 582
    returned value = 582

  7. #7
    Registered User
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    5
    Every time I do something other than srand (time(NULL)), it messes with the while condition. @nonpuz, even in your code it bypassed numbers that met the criteria. This is what is happening every time.

    Sorry if I'm taxing your patience, I do appreciate all the input. I have not actually been taught C. We were given some basics and told to pick up what ever we could on our own. This is all part of the classic "unisex bathroom" problem. I am using the modulus to determine if they are male or female.
    Last edited by Kent Luttrell; 12-08-2012 at 04:36 PM. Reason: thanks, and explanation

  8. #8
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Thats because the first condition I gave you was still wrong (Although I originally posted it correctly then changed it to the wrong thing )

    Here is the correct condition for your loop to achieve what you need:
    Code:
        }while ((z%2) != 0 && (z%3) != 0);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if condition
    By jgtech in forum C Programming
    Replies: 1
    Last Post: 04-12-2011, 05:25 AM
  2. If condition
    By rits in forum C Programming
    Replies: 3
    Last Post: 09-02-2009, 05:54 AM
  3. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 08:48 PM
  4. need while(for(loop)) to be condition
    By thestien in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2006, 08:32 AM
  5. c++ condition use? how u use it? help
    By mikeasianlee in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 08:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21