Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help (i cant say much more)

    here is the basics of my code

    #include <windows.h>
    #include <time.h>

    int Chance;
    int x;

    int compress()
    {
    if (Chance < 500){
    x = 1;
    }
    else if (Chance < 990){
    x = 2;
    }
    else if (Chance < 997){
    x = 3;
    }
    else if (Chance < 998){
    x = 4;
    }
    else if (Chance <999){
    x = 5;
    }
    else if (Chance == 999){
    x = 6;
    }

    return x;
    }
    int main()
    {
    srand(GetTickCount());
    for( ; ; )
    {
    Chance = rand() % 1000;
    Sleep(rand() % 60000);

    switch(x){
    case 1:
    bla bla bla
    break;
    case 2:
    bla bla bla
    break;
    case 3:
    bla bla
    break;
    case 4:
    bla bla bla
    break;
    case 1:
    bla bla bla
    break;
    default case:
    bla bla bla
    break;
    }
    }
    return 0;
    }
    my question is will it work and for every loop will it come up with a different number.

    also is there an easier way to have events and have there be for example
    10% chance event 1 happens
    10% chance event 2 happens
    20% chance event 3 happens
    60% chance event 4 happens

    any help is appreciated
    Last edited by schmalx; 07-19-2007 at 11:29 AM. Reason: get rid of smiley ;) + un needed header

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Well why don't just compile it and try it? See if it does what you expect?

    Also you never set the global variable x and you shouldn't use a for loop like that.
    OracleGuy

  • #3
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    There's nothing wrong with using a for loop like that imo. I prefer it to while(1) for event loops, which is basically what this is except it's making up its own events. This will not work as you expect because the stock implementation of rand() has a very short period... it'll cycle through the same set of values again and again, and will only look random on scales shorter than the period. If this is for a game or something that needs to run for a bit with lapsing into predictability you'll need to explore third-party random number libraries or (not recommended) roll your own.

  • #4
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oracleguy View Post
    Well why don't just compile it and try it? See if it does what you expect?
    I didnt have a compiler at the time

    Also you never set the global variable x
    lol yeah
    and you shouldn't use a for loop like that.
    why not?


    also i made program that will change the colours of all the pixels on the screen to black in a continuous loop.
    now i have noticed that if i go to switch user and log back on as the same person the program is still running but it is doing nothing.
    why is this?
    and how can i stop this from happening?
    Last edited by schmalx; 07-20-2007 at 09:46 AM.


  •  

    Posting Permissions

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