Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Infinite loop help

Status
Not open for further replies.

rewclaus

Programmer
Mar 7, 2005
16
US
Hi again. I am stuck with an infinte loop:

Code:
int arr[18];  //an array of 18 integers (Iadding integers later)
int deck[60]; //empty to start
int j = 0;
int r = 0;
int u = 0;
int c = 0;
int i = 0;

deck[0] = 1;

for (i = 1; i < 60; i++)
{

 j = 0;

 while (j == 0)
   {

    x = ((323 * rand()) / (RAND_MAX)) + 7; //choose 'x' to be a random number between 7 and 330

    k = (2 * rand()) / (RAND_MAX); //choose a random number between 0 and 2

    if (k == 1)
      { x = arr[((18 * rand())/(RAND_MAX))]; } //choose 'x' to be a random value of the array 'arr'

    printf("x = %d\n", x);

    if ( (x >= 7) && (x <= 114) )
      { r++; }

    if ( (x >= 115) && (x <= 222) )
      { u++; }

    if ( (x >= 223) && (x <= 330) )
      { c++; }

    if ( r > 3 )
      { continue; }

    if ( u > 20 )
      { continue; }

    if ( c > 36 )
      { continue; }

    j = 1;
   }

   deck[i] = x;
}

What I would like this to do is this:

When an 'x' is chosen it checks to see which set 'x' belongs to ('r', 'u', or 'c'). Depending on which set 'x' belongs to, I want that set to incrememnt. I never want 'r' to be above 3, 'u' to be above 20, or 'c' to be above 36. I want to add 59 values into 'deck', with the restriction that the value that is added cannot belong to a certain set more than the max value needed for that set (i.e. max val of 'r' is 3, so no more than 3 values that fall into the 'r' range can be added to the array 'deck'). Ultimately I want to end the program with an array ('deck') of 60 values (3 which fall into the 'r' category, 20 that fall into the 'u' category, 36 that fall into the 'c' category, and the number '1'). I hope this is understandable to everyone. If not, let me know and I will try to enlighten you.

Tnx
-rewclaus
 
Well the way you generate random numbers is broken
Code:
#include <stdio.h>
#include <stdlib.h>

int GetRand(int min, int max);

int main ( ) {
  int i;
  for ( i = 0 ; i < 20 ; i++ ) {
    int r = ((323 * rand()) / (RAND_MAX)) + 7;
    printf( "%d\n", r );
  }
  printf("\n");
  for ( i = 0 ; i < 20 ; i++ ) {
    int r = GetRand(7,330);
    printf( "%d\n", r );
  }
  return 0;
}

int GetRand(int min, int max)
{
  static int Init = 0;
  int rc;

  if (Init == 0)
  {
    /*
     *  As Init is static, it will remember it's value between
     *  function calls.  We only want srand() run once, so this
     *  is a simple way to ensure that happens.
     */
    srand(time(NULL));
    Init = 1;
  }

  /*
   * Formula:
   *    rand() % N   <- To get a number between 0 - N-1
   *    Then add the result to min, giving you
   *    a random number between min - max.
   */
  rc = (rand() % (max - min + 1) + min);

  return (rc);
}

//
My output
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7

281
95
68
255
51
215
167
308
207
172
55
297
233
128
24
72
175
155
77
185

Yours generate the same constant every time, so there's no way for it to progress to a solution.

--
 
Alas, the main loop while (j == 0) is wrong in principle. You have only to set r > 3 (or u > 20 or c > 36) and it never stops because of you never decrement these vars...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top