Divide a number into random parts

I need to divide a number into a collection of random parts. So that the sum of all parts is equal to the number.
E.g.:
public void divideRandomly ( int number, int parts)
int[] returnArray = new int[parts];
//do random division calculations
return returnArray;
Lets say the number = 20
And parts = 4
I would then like to return an array of ints containing e.g. 3, 11, 5, 1
OR e.g. 4,8,2,6
Does anyone have a clue as to how I could do this?
Cheers

Be aware that given the wrong parameters this solution
can take a long time to find a list. This seems misleading, as the termination condition is based
on randomness. Although, it is possible to predict an average
termination time.
Basically your algorithm starts by generating initial random values
in the range [0, maxValue]. You limit the size of the maximum
generated value, although I'm not sure why. Obviously maxValue
has to be greater than sum / length, otherwise it would be
impossible to generate any combination values that add up to sum.
Then your algorithm asks if the sum of those initial random values
is equal to the target sum. If not, you cycle through the list replacing
the the current position with a new random value and updating the
current sum.
As an example, let's suppose that maxValue is equal to sum, and
that sum = 1000, and length (numParts) is 4.
Assuming the minValue is 1, not 0, then the total number of
permutations of 4 numbers that add up to 1000 is:
165,668,499
However, the total number of permutations is:
1000^4 = 1,000,000,000,000
In terms of probability, the chance of finding a solution on each try is:
165,668,499 / 1,000,000,000,000 = 0.000165668499
Thus, you'll need approximately 6036 loops on average to find a solution for 1,000 split 4 ways.
For 2,000 split 4 ways, number of perms is:
2000^4 = 16,000,000,000,000
The perms of 4 numbers that add up to 2000 is:
1,329,336,999
Probability of finding a solution on each try is:
0.0000830835624375
Average number of tries is: 12036
I'm impressed that the random approach is that fast.

Similar Messages

  • How can I divide a number into bytes

    I tried the icon called "split number". It didn't work for me.

    Pushpa,
    "split number" is certainly the VI that I would recommend you use. Keep in mind that it splits the input into two equal parts. If you input a 32-bit integer, the high 16 bits and low 16 bits correspond to the two outputs. You'll then need to split one of the previous outputs into its high and low components to access each individual byte.
    You could also construct a mask, bitwise AND it with your number, and then shift the result the appropriate number of bits. However, for the sake of simplicity, I recommend that you use "Split Number."
    Alan
    Applications Engineer
    National Instruments

  • Split float into two parts

    I have the following code to split a float number into 2 parts (as integer):
         float f = 1.2345f;
    String s = String.valueOf(f);
         int indexOfPoint = s.indexOf(46);
         String si = s.substring(0, indexOfPoint);
         String sf = s.substring(indexOfPoint+1,s.length());
         int intPart = Integer.valueOf(si).intValue();
         int fracPart = Integer.valueOf(sf).intValue();
    But this won't do if the float is in scientific notation, the last last line will return a java.lang.NumberFormatException. Can anyone help me? Thank you very much.

    I'd advise doing the split between fractions and integers mathematically, rather than through string manipulation.
    OTOH if the split is purely for display purposes, perhaps using java.text.DecimalFormat would help.

  • Dividing Genrated PDF no  of  pages into two parts

    Dear Friends,
    I have requirement in which i need to divide total number output pdf pages into two parts.
    E.g. If there are 10 pages Then   10 / 2 = 5
    Before generation of pdf file from smartform it automatically divide by 2 and then show these files.
    How to do this?
    Thanks & Regards,
    Ravi Grover

    Hi Rob,
    when you call the function module of the smartform you have the output parameter:
    DOCUMENT_OUTPUT_INFO-TDFPAGES
    It is the number of pages of the smartform.
    Then... you can call the funtion module of smartform two times  :
    1rst with the input field OUTPUT_OPTIONS-TDPAGESLCT from 1 to number of pages / 2
    2nd  with the input field OUTPUT_OPTIONS-TDPAGESLCT from  number of pages / 2 to number of pages
    And generate both pdf.
    Bye!

  • When ever  enter the date start date up to next year same date between the days divided into 8 parts

    when ever  enter the date start date up to next year same date between the days divided into 8 parts
    Q1.1 (YYYY) = 1st half of Quarter 1 for year YYYY
    Q1.2 (YYYY) = 2nd half of Quarter1 for year YYYY
    Q2.1 (YYYY) = 1st half of Quarter 2 for year YYYY
    Q2.2 (YYYY) = 2nd half of Quarter 2 for year YYYY
    Q3.1 (YYYY) = 1st half of Quarter 3 for year YYYY
    Q3.2 (YYYY) = 2nd half of QuarterQ3 for year YYYY
    Q4.1 (YYYY) = 1st half of Quarter 4 for year YYYY
    Q4.2 (YYYY) = 2nd half of Quarter 4 for year YYYY
    Here YYYY depicts the year.
    e.g. Q1.2 (2014) depicts the 2nd half of Quarter 1 for year 2014.
    The description of these values are explained below.
    The table below provides the description about each value:
    Quarter     Quarter Range      Start Date
    Q1.1      1 Jan - 15 Feb         1st  Jan
    Q1.2      16 Feb-31 Mar         16th Feb
    Q2.1      1 Apr- 15 May          1st Apr
    Q2.2      16 May-30 June       16th May
    Q3.1      1 Jul-15 Aug             1th Jul
    Q3.2      16 Aug -30 Sep       16th Aug
    Q4.1      1 Oct -15 Nov           1st Oct
    Q4.2      16 Nov – 31 Dec      16th Nov
    The dropdown values in time window needs to be updated as per date entered by the user in the Audit Plan start date and
    should display the next four Quarter (each divided in 2 half  i.e. Eight values ) along with the year  from the selected Audit plan start date.
    for eg. If the Plan start date is given as August 10 2013 then the Time window will display the following options:                      
    Q3.2 (2013)                
    Q 4.1 (2013)               
    Q 4.2 (2013)               
    Q 1.1 (2014)               
    Q1.2 (2014)                
    Q2.1 (2014)                
    Q 2.2 (2014)               
    Q 3.1 (2014)               
    You can refer to the Table above and look that 10 Aug 2013 falls under the Q3.1 so Time window will display the next next 8 half Quarters ( Total 4 Quarter) till Q 3.1 for the year 2014.

    Hello,
    WITH half_quarters AS(
        SELECT  ADD_MONTHS(TRUNC(DATE '2013-08-15','Q'), 3*(LEVEL - 1)) hq_start
               ,1 part
        FROM    dual
        CONNECT BY ROWNUM <= 5
        UNION ALL
        SELECT  ADD_MONTHS(TRUNC(DATE '2013-08-15','Q'), 3*(LEVEL - 1) + 1) + 15 hq_start
               ,2 part
        FROM    dual
        CONNECT BY ROWNUM <= 5
    ,ordered_half_quarters AS(
        SELECT  hq_start
               ,part
               ,ROW_NUMBER() OVER (ORDER BY hq_start) r
        FROM    half_quarters
        WHERE   hq_start > DATE '2013-08-15'
    SELECT  'Q '||TO_CHAR(hq_start,'Q')||'.'||part||' ('||TO_CHAR(hq_start,'YYYY')||')' q
    FROM    ordered_half_quarters
    WHERE   r <= 8
    ORDER BY r;
    Q       
    Q 3.2 (2013) 
    Q 4.1 (2013) 
    Q 4.2 (2013) 
    Q 1.1 (2014) 
    Q 1.2 (2014) 
    Q 2.1 (2014) 
    Q 2.2 (2014) 
    Q 3.1 (2014) 
    half_quarters generates the start dates of every half of a quarter, starting with the begin of the first quarter that contains the sample date.
    The next step is to order the dates and to select only those after the sample date.
    The last part formats the output and orders the data.
    Regards
    Marcus

  • I would like to ask about imovie. my project cannot output and how can i divide my project in imovie into two part??, i would like to ask about imovie. my project cannot output and how can i divide my project in imovie into two part??

    i would like to ask about imovie. my project cannot output and how can i divide my project in imovie into two part??, i would like to ask about imovie. my project cannot output and how can i divide my project in imovie into two part??

    Go to the Project Library View and select your project.
    Then FILE/DUPLICATE PROJECT and give the duplicate a name like "your name - part 1".
    Then duplicate it again and give the duplicate a name like "your name - part 2"
    Now you can open "part 1" as a project and delete everything that is in part 2.
    Open "part 2" as a project and delete everything that is in part 1.

  • Dividing string into specific parts

    Hi, I am trying to write a program that will convert a domain name into a Java package name, for instance forum.java.sun would be sun.java.forum.
    I have the scanner class search for the periods, but all that does is find the index of the periods for me. Is there a way to divide the domain name into three parts? If so, would I have to store them in three seperate variables and then used the reverse() function to switch them around?
    Thanks.

    Hi, I am trying to write a program that will convert
    a domain name into a Java package name, for instance
    forum.java.sun would be sun.java.forum.
    I have the scanner class search for the periods, but
    all that does is find the index of the periods for
    me. Is there a way to divide the domain name into
    three parts? If so, would I have to store them in
    three seperate variables and then used the reverse()
    function to switch them around?Scrap scanner and simply use String.split("\\.") to get the parts as an array.

  • I need to divide selected row into multiple rows when i navigate  ADF 11g

    Hi
    I'm using jdeveloper 11.1.1.2.0 with ADF 11g.
    I need to divide selected row into multiple rows when i navigate to other page . Scenario - in first page i'm displaying some records with columns like empno , empstatus , empworkdepts ,curdepts
    Here empworkdepts gives the numeric number like no of departments work shifts 3 or 4 or 5. when i select any particular employee and fire next button to navigate next page.I have to divide the selected employee with same information into multiple times based on the empworkdepts value.
    empno empstatus empworkdepts curdept
    001 eds 2 TS
    002 hr 1 FO
    003 eds 4 TS
    *004 eds 3 TS*
    now i selected employee 004 , when i navigate to next page.
    Empno EmpStatus EmpWorkDepts CurDept
    004 eds 3 TS
    004 eds 3 TS
    004 eds 3 TS
    i did with java code in bean .but not stable .
    any help............
    thanks advance.............
    Edited by: user9010551 on May 5, 2010 10:48 PM
    Edited by: user9010551 on May 10, 2010 11:31 PM

    user9086775 wrote:
    Hi Experts,
    I have a requirment where i need to fetch parts of a single row into multiple rows from a singlt Query, i am sure it is possible using Pivots but just cant figure out an approach. Any help on this is highly appriciapted.
    Requirment:
    This is a sample set record in a table
    Product     Sub Product          Name    Age
    New Car    Nissan                   Tom        49
    New Car    Nissan                   Jack         36
    Old Car      Audi                     Sam         24
    Old Car      Jaguar                  Pint          26
    Old Car      Audi                     Smith       41
    I need to be able to fetch the above data in the below fashion
    Product     Sub Product          Name    Age
    New Car
    Nissan
    Tom        49
    Jack        36
    Old Car     
    Audi            
    Sam        24
    Smith      41
    Jaguar                   Pint         26Please help with ideas as to how can i achive the above without using PLSQL.
    Thanks in advance!You should be doing this in the client on not in the DB. Use the reporting tool that you use to do this.
    For example if you are in SQL Plus you can use the BREAK command.

  • How To Divide Source File Into Smaller Files Without Exporting?

    Hello Everyone,
    I'm a beginner with PPro CS4 and to video editing in general. I've done a lot of Googling and searching of these forums to get an answer to this question, but I haven't found a clear answer. Here's my problem:
    I have many hours of HDV footage shot over the past few years with a Canon HV30. This footage is on dozens of DV tape cassettes. I want to capture most of it to disk.
    When I capture a tape to disk, I get a single large mpeg4 file. (PPro captures all HDV footage to mpeg4.) "Scene select" doesn't work with the HV30, so my only other way to make smaller capture files would be to set In/Out points with batch capture. But this would mean hours of fast-forwarding and reversing with the VCR-like controls on the camera to review all the tapes and set the points. I thought it might be FASTER to simply capture an entire tape in one operation, THEN review the captured tape within PPro (it's much faster using the scrubber), and dividing that file into smaller files within PPro, thereby avoiding all the mechanical fast-forwarding and rewinding on the camera itself.
    But the only way I can find to do this seems to involve EXPORTING clips. Am I wrong to think that exporting a clip to a new file involves a degradation of the original mpeg4 source file? Or will there be no loss to the quality if I simply export to mpeg4 format?
    It seems there should be an easy way to simply divide the original capture into smaller source files for later editing, without putting the footage through another generation of processing (exporting), which might entail some loss of quality. Or am I wrong about this?
    (CS4 Master Suite, i7 3630k, 16GB RAM, C: 256GB SSD, D: 1T Setpoint F3, E: 1T Setpoint F3)

    HDV footage is mpeg2 (whats in a number ). CS4 does not do scene detect you need to use HDV split for that.
    http://strony.aster.pl/paviko/hdvsplit.htm.
    You can run your already captured files through hdv split and it will chop the file into clips and off you go.

  • Dividing a project into two projects

    How do I divide my project into two projects so that I can burn it to a DVD? I have 2hrs 40 mins and this is too long. thanks!

    Go to the Project Library View and select your project.
    Then FILE/DUPLICATE PROJECT and give the duplicate a name like "your name - part 1".
    Then duplicate it again and give the duplicate a name like "your name - part 2"
    Now you can open "part 1" as a project and delete everything that is in part 2.
    Open "part 2" as a project and delete everything that is in part 1.

  • Select a fixed number of random rows

    Hi,
    I just would like to select a fixed number of random rows from a table. That is, I would like to get 5 rows, without specifying the row_id, and the selected rows should "always" be different. Just as picking 5 balls out of a big box (which is filled with many balls).
    Is there a way to easily put it into one sql statement or do I have to write a workaround in the programming language I use?
    Thanks a lot in advance,
    CQ

    AskTom has a discussion on this:
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:6075151195522
    Note that it requires that you
    1) Generate a random number for every row in the table
    2) Sort the table by those random numbers
    which is a significant performance hit if the table is large.
    Justin

  • Why do we divide a servlet into many methods?

    Hello;
    sometimes i see that we divide a servlet into many methods...these methods are related with other methods in same servlet and sending parameters to each other...at last all the methods used at least once..and returning variable number is only one...
    so why do we seperate these methods?we can do the same job in one method...
    i wanted to learn the reason?is it a programming tecnique or the way of creating a library?or related with performance...?
    but my question is different...i am asking when all the methods are used in a class why do we seperate them as methods?
    thank you

    can you post one such example ?

  • Dividing a photo into 2 or 4 even sections in Elements 10

    How would i go about dividing a photo into 2or 4 even separate photos that can be assembled back together? i am working on an art project that i need to transfer a large photo but my printer only prints 8x10 so i want to divide photo into 4 and reassemble on canvas.

    Brad,<br />It wasn't clear if you want to cut the picture into pieces jig-saw style so that the whole is the sum of its parts. If so here is one way using Elements 2 (maybe newer versions have ways of doing this more easily).<br /><br />In this example I cut the picture into 3 parts using the Lasso.<br />1. Select part 1.<br />2. Select > Save Selection: give the selection a name (e.g., part 1).<br />3. Copy/paste selection to new layer (Layer 1 in my example).<br />4. Activate Background layer.<br />5. Select > Reselect, and invert the selection.<br />7. Holding down <Alt>, use Lasso to delete all but part 2.<br />8. Select > Save Selection: give the selection a name (e.g., part 2).<br />9. Copy/paste selection to new layer (Layer 2 in example).<br />10. Activate Background layer.<br />11. Select > Reselect.<br />12. Select > Load Selection: In the Selection pull-down menu choose part 1 and check the "Add to Selection" box. The selection will now consist of parts 1 & 2.<br />13. Invert the selection to give part 3 and copy/paste to new layer (Layer 3 in example).<br /><br />The 3 pieces are in separate layers with transparent background. To save each piece turn off visibility of the other layers.<br /><br />http://www.pixentral.com/show.php?picture=1Q7SCAqb8IbQhcV6OdIWvj24WkzB5<br /> <br />Note that for just 2 parts it is much easier; you don't need the Save Selection steps:<br />1. Select part 1 and copy/paste to new layer.<br />2. Activate Background layer.<br />3. Select > Reselect, invert the selection, and copy/paste to new layer.

  • Do I have any function/way to divide the rows into un-equal size buckets?

    Just like the function NTILE divides the rows into almost equal size buckets, do we have any way to divide the rows into un-equal (randomly decide) buckets so that eg. I can get 4 rows in bucket_1, 38 rows in bucket_2, 17 in bucket_3, ..... Only things is that each bucket should have rows from 1 to 50. (fixed limit).

    with source_table as (select object_type, object_name from all_objects where rownum <= 100),
         t            as (select rownum as rn, s.* from source_table s),
         cnt          as (select count(*) cnt from t),
         bucket       as
         ( select bucket_id, bucket_width,
               sum(bucket_width) over (order by bucket_id) - bucket_width + 1 as rn_start
             from
             ( select level as bucket_id, ceil(dbms_random.value(0, 50)) as bucket_width
                 from cnt
                 connect by level <= cnt
    select bucket_id, object_type, object_name from t, bucket
      where rn between rn_start and rn_start + bucket_width - 1
      order by bucket_id
    ;

  • IChat is dropping me into random chatrooms

    This has been happening lately and I was wondering if anyone else has experienced this. Lately when I sign on I've been getting people IM'ing me asking me who I am. Apparently I've been just popping up into random chatrooms. Is it iChat or is it my AIM account? And what can I do to stop this. I can't actually see the chatrooms...I just get the IM's of people wondering who I am. Any info would be great. Thanks.

    Deselect "Remember Chats across Openings" In the Messages part of the Preferences
    This will stop ichat opening Old chats.
    Also check the File Menu and "Go to Chatroom" and see what is in the list and the Auto join status
    You should be able to delete these.
    12:03 PM Sunday; March 2, 2008

Maybe you are looking for

  • How to extract a specific group of data for printing

    New to the forums here, I am using LiveCycle Designer ES 8.2, and am trying to find out how I would have data entered into my form extracted to be printed separately.  In more detail, I have a form that users would fill out then submit to a help desk

  • 10.6.7 and 10.6.8 crash my Mini 2010

    Mini works great with 10.6.6. Have upgraded my MBP without incident. Has same software config as the Mini. 10.6.7 finder becomes unresponsive, machine will not wake from sleep. Restored from time capsule back to 10.6.6. Tried 10.6.8 yesterday. One by

  • How can I perform the conversion of pdf files in Cyrillic script to Word files in Cyrillic script. The pdf file is too small for me to read right now. Julyan Watts

    How can I perform the conversion of .pdf files in Cyrillic script to Word files in Cyrillic script. The .pdf file is too small for me to read without a magnifying glass, and the document is more than one thousand pages.

  • Capital one Approval!

    So i'm working on rebuilding my credit after getting my chapter 7 discharge in february. I was planning on opening a second secured card next week, but decided to go ahead and try the capital one pre qualifier just for the hell of it. To my surprise

  • Layout and resize

    Although I tried many things, I can't get out of this : I have a java app. with a jSplitPane. On the right side, I have a JPanel hosting another jPanel with an image background. What I'd like is that when moving the divider, my inner Panel (the one w