Thread: Help

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    4

    Help

    Hello,
    first of all i must say that i'm new in programming and especially in c++.also i m sorry for my bad english..i read somewhere for function rand and i have some questions about that.i read that to find random numbers the type is x=low+rand()%(high-low+1).That i get it but how i can to have some exceptions.For exaple to find random numbers between -10 and 10 apart of 0...What i did was to wrote x=rand()%21-10;
    x!=0;
    but still appeared the 0 and i don t know what to do.
    thanks in advance

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Code:
    x = rand() % 20;
    if (x < 10)
    {
        x -= 10;
    }
    else
    {
        x -= 9;
    }
    or
    Code:
    do
    {
        x = rand() % 21 - 10;
    } while (x == 0);

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    4
    Quote Originally Posted by DRK View Post
    Code:
    x = rand() % 20;
    if (x < 10)
    {
        x -= 10;
    }
    else
    {
        x -= 9;
    }
    or
    Code:
    do
    {
        x = rand() % 21 - 10;
    } while (x == 0);

    Thank you very much!!!What was my mistake??

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What was your actual code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed