Thread: Random numbers.

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Random numbers.

    Is their a way to have a user input a number and to have that number be the size of an array and have it produce random numbers in the array that equal one number.

    Lets say the user inputs 7
    I am trying to figure a way to then make a function that takes that number then produce seven randomly selected numbers into an array that when added would equal a certaon number every time.
    Lets say i want it to equal 50 it would produce the numbers 4, 7, 21, 8, 2, 3, 5. these add up to fifty. This is probably very confusing the way i described the problem but if you understand and know what i am talking about then your help would be greatly appreciated.

    Thanks,
    Nate

  2. #2
    YES.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    What you need is meory dynamics (it's very simple...)

    First, you get the number, say 7. Then you have this line in your code:
    Code:
    int *array;
    array = new int[number_that_is_from_user];
    (After you have finished with array you must destroy it since it's a pointer: delete [] array;

    Now, when you want to have that array full of random numbers that equal to n, things get complicated. Statistically speaking the best way might be a loop:

    Code:
    int sum;
    while(1)
    {
      sum = 0;
      for(int i = 0;  i < number_that_is_from_user;  i++)
      {
        array[i] = rand() % max;
        sum += array[i];
      }
      if(sum == what_we_want)
        break;
    }
    Only problem is that as arrays get bigger, the time needed to break from that loop gets huge!

    Other simple option is that you do something like:

    Code:
    int max = 50, sum = 0;
    for(int i = 0;  i < number_that_is_from_user;  i++)
    {
      arrray[i] = rand() % (max-sum);
      sum -= array[i];
    }
    Only problem is that array[0] has the biggest change to be biggest number, array[1] second biggest, etc. It wouldn't surprise me, that when array[0] = max then every other `random' number would be 0.

    Hopefully someone else has better ideas... :-)
    Last edited by kooma; 01-26-2002 at 04:32 AM.

  4. #4
    Unregistered
    Guest
    after you get the number of random numbers to use and the value you want the numbers to add up to you can keep varying the varlues you want. Let's say you want 5 random numbers between 0 and 50 (inclusive) to add up to total of 50. That means the largest value the first number can be is 50. If the first number is 50 then all the others have to be 0. If the the first number is 3 then the other 4 can only add up to 47 so limit the next random number vary from 0 to 47. If the second number is 47 then the other 3 can only be 0. If the second number is 10 then the largest value the third number could be is 50 - 3 - 10 or 37 so limit it to between 0 and 37. If the third number is 37 then the other 2 can only be 0. If the third number is 21 then the largest number the 4th number can be is 50 - 3 - 10 - 21 or 16 so limit the random number generator to 0 to 16. The last number cannot be random if all the other numbers are random and the desired total is fixed, so calculate what the fifth number has to be. Now you can extrapolate this to any number of random numbers and any desired target number.

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    If the user input was 7:

    Make the first 6 numbers random numbers with a max limit. Then, make the last number whatever is left to create the sum. Apply this concept to all numbers.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    Just pointing out

    Yeah, I don't think anyone explicitly mentioned (although everyone implied) that you can't do this:

    Code:
       any_type array_name[ variable ];
    The "variable" portion really can't be a variable. It has to be a object that can be resolved during compile-time. In other words, it can be an actual number, const object, definition from #define, etc.

    Even if you do know, someone browsing the thread may not. Just pointing it out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM