Results 1 to 11 of 11

Thread: Random shape height

  1. #1

    Thread Starter
    New Member uttamsaxena's Avatar
    Join Date
    Mar 2001
    Location
    India
    Posts
    13

    Talking

    I want to generate four random integers with a total sum of 100 like (12, 43, 18, 27) and then want to adjust the hight of four rectangle shapes in the same ratio i.e. 12:43:18:27.The above set of numbers is to be generated randomly to change the hight of rectangle shapes. What I actually want is to display a graph of 4 colums with random value in percentage.
    Please help.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    generate 4 numbers with RND function, with accuracy you choose, calculate their sum and divide it by 100 and you have the ratio which you divide each number with as a final. Use Int() function to truncate
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    kedaman,

    Just wondering, if you are trying to get 4 integers which sum to 100, and you generated 4 random numbers where the following shows the % results:

    number 1 : 23.3%
    number 2 : 23.3%
    number 3 : 24.3%
    number 4 : 29.0%
    ------
    100.0%

    What would the 4 integer numbers be?

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    23,23,24 and 29 would sum up 99, if you want to generate true random (that is equal chance for each number to have any value proportional to each other) then regenerate until you reach 100. Otherways you could add up the rest to a randomly choosed value.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    How 'bout this:

    Code:
    Dim A, B, C, D
    
       A = Int(Rnd * 100)
       B = Int(Rnd * 100)
    
       Do
          If A + B > 100 Then
             B = Int(Rnd * 100)
          End If
       Loop Until A + B =< 100
    
       C = Int(Rnd * 100)
    
       Do
          If A + B + C > 100 Then
             C = Int(Rnd * 100)
          End If
       Loop Until A + B + C =< 100
    
       D = Int(Rnd * 100)
    
       Do
          If A + B + C + D > 100 Then
             D = Int(Rnd * 100)
          End If
       Loop Until A + B + C + D =< 100
    And there you have it!!
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    thats definitely biased to give higher values for A and B
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Try this:

    Code:
    Dim Total, Temp, Val(1 to 4), CurVal
    
    Total = 100
    
    Do Until Total=0
        Temp = Int(Rnd*1)
        CurVal = CurVal + 1
        If CurVal > 4 Then Curval = 1
        Val(CurVal) = Val(CurVal) + Temp
        Total = Total - Temp
    Loop
    Heh, hope that works
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    biased to give values close to 25
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Ok, ok: this one should work

    Code:
    Dim Total, Temp, Val(1 to 4), CurVal
    
    Total = 100
    
    Do Until Total <= 0
        Temp = Int(Rnd*25)
        If Temp > Total Then Temp = Total
        CurVal = CurVal + 1
        If CurVal > 4 Then Curval = 1
        Val(CurVal) = Val(CurVal) + Temp
        Total = Total - Temp
    Loop
    Try it out. It generates completely random numbers
    Last edited by Jotaf98; Apr 24th, 2001 at 03:09 PM.
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Give up, and save yourself some time
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11

    Thread Starter
    New Member uttamsaxena's Avatar
    Join Date
    Mar 2001
    Location
    India
    Posts
    13
    Thanks for the help extended to me. I succeeded in doing it throgh the following code.


    Randomize
    Cls
    JA0 = Int((101 * Rnd) + 1)
    JA1 = Int((101 * Rnd) + 1)
    JA2 = Int((101 * Rnd) + 1)
    JA3 = Int((101 * Rnd) + 1)
    JT = JA0 + JA1 + JA2 + JA3
    JS0 = Int((JA0 / JT) * 100)
    JS1 = Int((JA1 / JT) * 100)
    JS2 = Int((JA2 / JT) * 100)
    JS3 = Int((JA3 / JT) * 100)
    JH0 = Int((JS0 * 120 * 31) / 100)
    JH1 = Int((JS1 * 120 * 31) / 100)
    JH2 = Int((JS2 * 120 * 31) / 100)
    JH3 = Int((JS3 * 120 * 31) / 100)
    FrmPubopn!Shape1.Top = 3720 - JH0
    FrmPubopn!Shape2.Top = 3720 - JH1
    FrmPubopn!Shape3.Top = 3720 - JH2
    FrmPubopn!Shape4.Top = 3720 - JH3
    FrmPubopn!Shape1.Height = JH0
    FrmPubopn!Shape2.Height = JH1
    FrmPubopn!Shape3.Height = JH2
    FrmPubopn!Shape4.Height = JH3
    FrmPubopn.Show
    End Sub


    What I have done-
    Generate 4 random nos range 0 to 100.
    get their sum.
    Devide each no by the sum and multiply by 100 to get percentage.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width