heh65532

Members
  • Content count

    49
  • Joined

  • Last visited

Community Reputation

171 Neutral

About heh65532

  • Rank
    Member

Personal Information

  • Interests
    Programming
  1. random selection of array elements

    Oops I missed what you said there.. Got it working now! thanks!
  2. random selection of array elements

    I ran to a problem with the latest code I posted. While arrays like this work well: int array[asize] = {0, 1000, 2000, 3000}; it doesn't work anymore if the values are closer to each other like this: int array[asize] = {0, 1000, 2000, 2001}; The 2001 has only small change of being picked while it should have about same change of being picked than the 2000. So not sure how to handle that.
  3. random selection of array elements

    Thank you all for the replies. @alvaro that's just what I needed, you approach is so much better than mine. here's the resulting code, I think this is what you meant: #include <iostream> #include <string> using namespace std; int main() { srand (time(NULL)); int asize = 4; int array[asize] = {0, 1000, 2000, 3000}; int total = 6000; int ra = rand() % total; for(int i = asize - 1; i >= 0; i--) { if( array[i] <= ra ) { cout << "selected " << i << " --- " << array[i] << " <= " << ra << endl; break; } } cout << "finished" << endl; }
  4. random selection of array elements

    Actually I think the code I posted earlier works like supposed to. Here's my final code with array (I'll let the code show what I'm trying to archive): #include <iostream> #include <string> using namespace std; int main() { srand (time(NULL)); // Array already sorted int array[] = {200,300,700,1000,1500}; int asize = 5; int maxNum = array[asize - 1]; bool picked = false; for(int i = 0; i < asize; i++) { float change = (float)array[i] / ((float)maxNum) / 2.0; int r = rand() % 100; int ichange = (int(change * 100)); cout << " change: " << ichange << "% random: " << r << "%" << endl; if( r <= ichange ) { cout << " Picked random array element: " << array[i] << " with change of " << ichange << "%" << endl; picked = true; break; } } if(!picked) { cout << "Max picked: " << array[asize - 1] << endl; } } Not 100% sure if it really works mathematically correct so please let me know what you think.
  5. random selection of array elements

    Not using C++ just running tests with it. thanks though!
  6. random selection of array elements

    Ok here's the deal, I have been working on code to do this but it doesn't seem right, I'm unsure if I got the math right. I made sample program where there's no arrays but I'm trying to calculate the probability for each number. 100 / 100 for example should have 50% probability. Or in another example max number being 100 the number 10 should have 10% change of being picked. (max number just means the highest number found in array) here's the program: // Example program #include <iostream> #include <string> using namespace std; int maxNum = 100; int main() { for(float i = 0; i <= maxNum; i += 10) { float change = i / ((float)maxNum) / 2.0; cout << (int)i << " / " << maxNum << " = " << change << " " << ( int(100 * change)) << "% " << endl; } } Which outputs: 0 / 100 = 0 0% 10 / 100 = 0.05 5% 20 / 100 = 0.1 10% 30 / 100 = 0.15 15% 40 / 100 = 0.2 20% 50 / 100 = 0.25 25% 60 / 100 = 0.3 30% 70 / 100 = 0.35 35% 80 / 100 = 0.4 40% 90 / 100 = 0.45 45% 100 / 100 = 0.5 50% where the final (100 / 100) seems to be correct result but the five first calculations doesn't seem right. 10 / 100 should be 10% but I'm not sure if I got that part right. any ideas how to make this work?
  7. random selection of array elements

    proportions are different to the numbers. all I need is select just one number from the array as long as it's random.
  8. random selection of array elements

    @Lactose I googled one article but no help yet from there, will continue looking. @sjhalayka Thanks that works! I wonder if there is another approach that's not so heavy... @Kylotan The higher the number the higher the change of being picked.
  9. Hello, I have a bit of programming dilemma which I cannot seem to solve my self. What if I have an array of numbers and I want to randomly select entry from the array so that the higher the number in the array the more likely it gets picked? For example array like this: [1000,800,400,200] Should have highest change of selecting the highest number (1000) and lowest change for the lowest number. How to calculate this? thanks!
  10. Thanks ErnieDingo. I went through those links and I believe only thing I need is this: matrix.setToRotation(new Vector3(0,0,1),-90); It works but the problem I'm having is that now all the sprites get rendered sideways. I could just rotate them but I believe there is cleaner way of doing this with matrixes. Or is there?   thx!
  11. HI I want to have matrix that makes the coordinates go X right and Y down. can someone provide me a matrix to do this? If I understand correctly this matrix goes to setTransformMatrix() in libgdx. sorry I'm not very good at matrixes.   thx!  
  12. Generic method in Java

    I have a question about "?" usage in method. I was watching tutorial where that was used with a list like this: void someMethod(List<? extends Number> numbers) {} But when I tried to get that same working for single object (not list) it would not compile. can the "?" not be used with single objects then?   this: void test(<? extends Number> num) { } gives error: "wildcards may be used only as reference parameters"
  13. Generic method in Java

      errors are in the comments of the code   So I need to learn reflection and erasure.   thx
  14. Hi I'm learning to code in Java and having background of C++ I tried to create this generic method but I get lot of errors and don't know how to make this code work. can someone show me how it's done?   the code: <T> Button createButton(String resourceName,String textName) { skin.add(resourceName, new Texture(textName)); T.TextButtonStyle buttonStyle = new T.TextButtonStyle();    // Error , cannot resolve buttonStyle.up = skin.getDrawable(resourceName); buttonStyle.down = skin.getDrawable(resourceName); buttonStyle.checked = skin.getDrawable(resourceName); buttonStyle.over = skin.getDrawable(resourceName); buttonStyle.font = skin.getFont("default-font"); Button button = new T("", skin, resourceName );   // Error T cannot be instantiated directly return button; } The code is supposed to make T be an different kind of Button object, for example TextButton. and then create button of that class and return it.   thx
  15. Hi I'm new to mobile developing (in fact I just started yesterday) and I want to create game with libGDX, my question is can you use the GUI you make with android studio with GDX? or if I choose GDX should I make my own GUI?   thanks