WHy my program doesn't work

Discussion in 'C++' started by Duardo Mattheo, Mar 24, 2007.

  1. #include<iostream.h>
    #include<stdlib.h>
    void main(void)
    {
    unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
    cout<<"Money summed = "<<(u);
    }

    thank you,
    Duardo
    Duardo Mattheo, Mar 24, 2007
    #1
    1. Advertising

  2. Duardo Mattheo

    Bo Persson Guest

    Duardo Mattheo wrote:
    :: #include<iostream.h>
    :: #include<stdlib.h>
    :: void main(void)
    :: {
    :: unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
    :: cout<<"Money summed = "<<(u);
    :: }
    ::
    :: thank you,
    :: Duardo

    Depending on the machine you have, 1 billion times some value is highly
    likely to be larger than what an unsigned int can store.


    Bo Persson
    Bo Persson, Mar 24, 2007
    #2
    1. Advertising

  3. Duardo Mattheo

    Colander Guest

    On Mar 24, 11:07 am, "Duardo Mattheo" <> wrote:
    > #include<iostream.h>
    > #include<stdlib.h>
    > void main(void)
    > {
    > unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
    > cout<<"Money summed = "<<(u);
    >
    > }
    >
    > thank you,
    > Duardo


    Because you have to write

    'std::cout' where you wrote 'cout'.

    Because you can't write a number with spaces in it.

    Because main has to return int.

    So the next will work:
    #include<iostream>
    #include<stdlib.h>
    int main(void)
    {
    unsigned int u=int(1000000000*rand()%RAND_MAX);
    std::cout<<"Money summed = "<<(u);

    return 0;
    }
    Colander, Mar 24, 2007
    #3
  4. Duardo Mattheo

    Colander Guest

    On Mar 24, 1:30 pm, "Colander" <> wrote:
    > On Mar 24, 11:07 am, "Duardo Mattheo" <> wrote:
    >
    > > #include<iostream.h>
    > > #include<stdlib.h>
    > > void main(void)
    > > {
    > > unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
    > > cout<<"Money summed = "<<(u);

    >
    > > }

    >
    > > thank you,
    > > Duardo

    >
    > Because you have to write
    >
    > 'std::cout' where you wrote 'cout'.
    >
    > Because you can't write a number with spaces in it.
    >
    > Because main has to return int.
    >
    > So the next will work:
    > #include<iostream>
    > #include<stdlib.h>
    > int main(void)
    > {
    > unsigned int u=int(1000000000*rand()%RAND_MAX);
    > std::cout<<"Money summed = "<<(u);
    >
    > return 0;
    >
    > }


    (Replying to one selfs, what does the world do to me)

    Now that we have the systax right, we can look at wat you are trying
    to do.

    I guess you want a random number between 0 or 1 and 1000000000.

    This is not what the programme does....

    Please lookup srand and rand in your manual.

    srand is a function that will give you a new/different random number
    each time you run your progrogramme.

    Doing a modulo operation makes sure that a number is in a range, in
    your case the range will be [0, RAND_MAX), and not [0, 1000000000).

    Good luck
    Colander, Mar 24, 2007
    #4
  5. On 2007-03-24 14:02, Colander wrote:
    > On Mar 24, 1:30 pm, "Colander" <> wrote:
    >> On Mar 24, 11:07 am, "Duardo Mattheo" <> wrote:
    >>
    >> > #include<iostream.h>
    >> > #include<stdlib.h>
    >> > void main(void)
    >> > {
    >> > unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
    >> > cout<<"Money summed = "<<(u);

    >>
    >> > }

    >>
    >> > thank you,
    >> > Duardo

    >>
    >> Because you have to write
    >>
    >> 'std::cout' where you wrote 'cout'.
    >>
    >> Because you can't write a number with spaces in it.
    >>
    >> Because main has to return int.
    >>
    >> So the next will work:
    >> #include<iostream>
    >> #include<stdlib.h>
    >> int main(void)
    >> {
    >> unsigned int u=int(1000000000*rand()%RAND_MAX);
    >> std::cout<<"Money summed = "<<(u);
    >>
    >> return 0;
    >>
    >> }

    >
    > (Replying to one selfs, what does the world do to me)
    >
    > Now that we have the systax right, we can look at wat you are trying
    > to do.
    >
    > I guess you want a random number between 0 or 1 and 1000000000.
    >
    > This is not what the programme does....
    >
    > Please lookup srand and rand in your manual.
    >
    > srand is a function that will give you a new/different random number
    > each time you run your progrogramme.
    >
    > Doing a modulo operation makes sure that a number is in a range, in
    > your case the range will be [0, RAND_MAX), and not [0, 1000000000).


    And I'd like to point out that the range of the values output by rand
    already is [0, RAND_MAX], after all that is what RAND_MAX means, the max
    number that rand can output. So the modulo (%) operator is not needed.
    Perhaps a / was intended so that the value will be between 0 and 1000000000?

    Notice also that you need to seed rand before usage, or there's a great
    chance that it will return the same value each time you run the
    application, you can use the current time to get a quite good (but not
    cryptographically secure) seed:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>

    int main()
    {
    srand(time(0));
    unsigned int nr = static_cast<unsigned int>(1000000000 *
    rand() / double(RAND_MAX)
    );
    std::cout << nr;
    return 0;
    }

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Mar 24, 2007
    #5
  6. On 2007-03-24 12:09, Bo Persson wrote:
    > Duardo Mattheo wrote:
    > :: #include<iostream.h>
    > :: #include<stdlib.h>
    > :: void main(void)
    > :: {
    > :: unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
    > :: cout<<"Money summed = "<<(u);
    > :: }
    > ::
    > :: thank you,
    > :: Duardo
    >
    > Depending on the machine you have, 1 billion times some value is highly
    > likely to be larger than what an unsigned int can store.


    It will probably be enough on a modern 32-bit machine. What the OP
    should watch out for is the fact that he/she is casting the result to
    int but assigning to an unsigned int.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Mar 24, 2007
    #6
  7. Duardo Mattheo

    SasQ Guest

    Dnia Sat, 24 Mar 2007 05:30:55 -0700, Colander napisa³(a):

    > #include<stdlib.h>


    #include <cstdlib>

    > std::cout<<"Money summed = "<<(u);


    Why 'u' in parenthesis?

    --
    SasQ
    SasQ, Mar 24, 2007
    #7
  8. Erik Wikström wrote:
    > #include <iostream>
    > #include <cstdlib>
    > #include <ctime>
    >
    > int main()
    > {
    > srand(time(0));
    > unsigned int nr = static_cast<unsigned int>(1000000000 *
    > rand() / double(RAND_MAX)


    I don't think that will work. Assuming that the largest value is about
    4Gib, you will overflow most of the data that rand() outputs when
    multiplying it by 1000000000, loosing most of your resolution. The
    following should work:

    unsigned int nr = static_cast<unsigned int>(
    (rand() * rand()) % 1000000000)

    Assuming that RAND_MAX*RAND_MAX >= 1000000000-1 (in actual math, not on
    a CPU's integer arithmetic set) this should work. And although (rand()
    * rand()) could overflow, it wouldn't matter as that information is not
    needed when trying to get a value in the range [0, 1000000000).

    > );
    > std::cout << nr;
    > return 0;
    > }
    >


    Adrian
    --
    _____________________________________________________________________
    \/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
    \ _---_ Q. What are you doing here? _---_ /
    \ / | A. Just surf'n the net, teaching and | \ /
    \__/___\___ learning, learning and teaching. You?_____/___\__/
    \/______[blog:__http://adrians-musings.blogspot.com/]______\/
    Adrian Hawryluk, Mar 24, 2007
    #8
  9. On 2007-03-24 18:02, Adrian Hawryluk wrote:
    > Erik Wikström wrote:
    >> #include <iostream>
    >> #include <cstdlib>
    >> #include <ctime>
    >>
    >> int main()
    >> {
    >> srand(time(0));
    >> unsigned int nr = static_cast<unsigned int>(1000000000 *
    >> rand() / double(RAND_MAX)

    >
    > I don't think that will work. Assuming that the largest value is about
    > 4Gib, you will overflow most of the data that rand() outputs when
    > multiplying it by 1000000000, loosing most of your resolution. The
    > following should work:
    >
    > unsigned int nr = static_cast<unsigned int>(
    > (rand() * rand()) % 1000000000)


    I was more thinking along the lines of this (notice the added parentheses):
    unsigned int nr = static_cast<unsigned int>(1000000000 *
    (rand() / double(RAND_MAX))

    It will not be able to produce all values in the range 0-1000000000, but
    then again, with that range who would notice :)

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Mar 25, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    838
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,665
    Smokey Grindel
    Dec 2, 2006
  3. Alex Buell
    Replies:
    6
    Views:
    309
    Alex Buell
    Feb 23, 2006
  4. BlackjadeLin
    Replies:
    2
    Views:
    1,124
    BlackjadeLin
    Jan 13, 2008
  5. PerlFAQ Server
    Replies:
    0
    Views:
    141
    PerlFAQ Server
    Apr 23, 2011
Loading...

Share This Page