random number generator

Discussion in 'C++' started by Meyer von Landenhausen, Sep 11, 2004.

  1. hi please take a look at the source:

    #include <ctime>
    #include <iostream>
    using namespace std;

    int main()
    {
    char buffer[3];
    int i,r,temp;
    int nums[50];


    for(i = 1; i < 50; i++)
    {
    r = (rand() % 49) + 1;
    temp = nums; nums = nums[r] = temp;
    }


    cout << "Your six lucky numbers are...\n";
    cout << itoa(nums[1], buffer, 10) << " ";
    cout << itoa(nums[2], buffer, 10) << " ";
    cout << itoa(nums[3], buffer, 10) << " ";
    cout << itoa(nums[4], buffer, 10) << " ";
    cout << itoa(nums[5], buffer, 10) << " ";
    cout << itoa(nums[6], buffer, 10) << " ";
    cout << endl;
    return 0;
    }


    whats wrong with it?
    btw the prog shall creat 6 different 2-digit long numbers.

    thx a lot guys
    Meyer von Landenhausen, Sep 11, 2004
    #1
    1. Advertising

  2. Meyer von Landenhausen

    osmium Guest

    Meyer von Landenhausen writes:

    > hi please take a look at the source:
    >
    > #include <ctime>
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > char buffer[3];
    > int i,r,temp;
    > int nums[50];
    >
    >
    > for(i = 1; i < 50; i++)
    > {
    > r = (rand() % 49) + 1;
    > temp = nums; nums = nums[r] = temp;


    temp has no specified initial content. Start by thinking why you would want
    to put an unknown value (which is not the same as random) in a specified
    address. My guesss: there is no good reason.

    > }
    >
    >
    > cout << "Your six lucky numbers are...\n";
    > cout << itoa(nums[1], buffer, 10) << " ";
    > cout << itoa(nums[2], buffer, 10) << " ";
    > cout << itoa(nums[3], buffer, 10) << " ";
    > cout << itoa(nums[4], buffer, 10) << " ";
    > cout << itoa(nums[5], buffer, 10) << " ";
    > cout << itoa(nums[6], buffer, 10) << " ";
    > cout << endl;
    > return 0;
    > }
    >
    >
    > whats wrong with it?
    > btw the prog shall creat 6 different 2-digit long numbers.
    >
    > thx a lot guys
    osmium, Sep 11, 2004
    #2
    1. Advertising

  3. "Meyer von Landenhausen" <> wrote in message
    news:...
    > hi please take a look at the source:
    >
    > #include <ctime>
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > char buffer[3];
    > int i,r,temp;
    > int nums[50];
    >
    >
    > for(i = 1; i < 50; i++)
    > {
    > r = (rand() % 49) + 1;
    > temp = nums; nums = nums[r] = temp;
    > }
    >
    >
    > cout << "Your six lucky numbers are...\n";
    > cout << itoa(nums[1], buffer, 10) << " ";
    > cout << itoa(nums[2], buffer, 10) << " ";
    > cout << itoa(nums[3], buffer, 10) << " ";
    > cout << itoa(nums[4], buffer, 10) << " ";
    > cout << itoa(nums[5], buffer, 10) << " ";
    > cout << itoa(nums[6], buffer, 10) << " ";
    > cout << endl;
    > return 0;
    > }
    >
    >
    > whats wrong with it?
    > btw the prog shall creat 6 different 2-digit long numbers.
    >
    > thx a lot guys


    Well quite a lot.

    1) The use of itoa is complete unnecessary, simply

    cout << nums[1] << " ";

    does what you want. This also means buffer is unnecessary.

    2) Your for loop is incorrect

    for(i = 1; i < 50; i++)

    does numbers from 1 to 49 (inclusive), probably what you wanted was

    for(i = 0; i < 50; i++)

    which at least does 50 numbers. Don't forget that arrays start at zero in
    C++.

    3) Most fundamentally though your code is not close to doing the job you
    want it to. If you just followed the code though with a paper and pencil you
    should be able to see that.

    It look to me that you are tying the random shuffle method for solving this
    problem, but you've coded it wrong. The random shuffle has three steps

    1) Create an array containing the numbers 1 to 50
    2) Go through the array one at a time, swapping each element with another
    randomly chosen element
    3) Print out the first six numbers in the array.

    You completely forgot about step 1, and you coded step 2 incorrectly.

    Have another go and post again if you are still stuck.

    john
    John Harrison, Sep 11, 2004
    #3
  4. Meyer von Landenhausen

    rossum Guest

    On 11 Sep 2004 04:34:29 -0700, (Meyer von
    Landenhausen) wrote:

    >hi please take a look at the source:
    >
    >#include <ctime>
    >#include <iostream>
    >using namespace std;
    >
    >int main()
    >{
    > char buffer[3];
    > int i,r,temp;
    > int nums[50];
    >
    >
    > for(i = 1; i < 50; i++)
    > {
    > r = (rand() % 49) + 1;

    If you are going to use rand(), you also need to know about srand()
    and how to use it.

    rossum

    > temp = nums; nums = nums[r] = temp;
    > }
    >
    >
    > cout << "Your six lucky numbers are...\n";
    > cout << itoa(nums[1], buffer, 10) << " ";
    > cout << itoa(nums[2], buffer, 10) << " ";
    > cout << itoa(nums[3], buffer, 10) << " ";
    > cout << itoa(nums[4], buffer, 10) << " ";
    > cout << itoa(nums[5], buffer, 10) << " ";
    > cout << itoa(nums[6], buffer, 10) << " ";
    > cout << endl;
    > return 0;
    > }
    >
    >
    >whats wrong with it?
    >btw the prog shall creat 6 different 2-digit long numbers.
    >
    >thx a lot guys


    --

    The ultimate truth is that there is no Ultimate Truth
    rossum, Sep 12, 2004
    #4
  5. Meyer von Landenhausen

    Howard Guest

    "osmium" <> wrote in message
    news:...
    > Meyer von Landenhausen writes:
    >
    > > hi please take a look at the source:
    > >
    > > #include <ctime>
    > > #include <iostream>
    > > using namespace std;
    > >
    > > int main()
    > > {
    > > char buffer[3];
    > > int i,r,temp;
    > > int nums[50];
    > >
    > >
    > > for(i = 1; i < 50; i++)
    > > {
    > > r = (rand() % 49) + 1;
    > > temp = nums; nums = nums[r] = temp;

    >
    > temp has no specified initial content. Start by thinking why you would

    want
    > to put an unknown value (which is not the same as random) in a specified
    > address. My guesss: there is no good reason.
    >


    That's not the problem. The temp variable is first used by assigning a
    value to it. That's perfectly valid.

    The problem with his use of temp is that he put the second two assignments
    into one statement, which is not what he wanted. I think that was probably
    a mistake in typing (either when posting or coding, I can't tell). Using
    better formatting, it should have been:

    temp = nums;
    nums = nums[r];
    nums[r] = temp;

    I suspect this is what was meant, but it got lost in the poor formatting.
    Another good reason to put separate statements on separate lines! :)

    -Howard
    Howard, Sep 13, 2004
    #5
  6. Meyer von Landenhausen

    osmium Guest

    Howard writes:

    > "osmium" <> wrote in message


    > news:...
    > > Meyer von Landenhausen writes:
    > >
    > > > hi please take a look at the source:
    > > >
    > > > #include <ctime>
    > > > #include <iostream>
    > > > using namespace std;
    > > >
    > > > int main()
    > > > {
    > > > char buffer[3];
    > > > int i,r,temp;
    > > > int nums[50];
    > > >
    > > >
    > > > for(i = 1; i < 50; i++)
    > > > {
    > > > r = (rand() % 49) + 1;
    > > > temp = nums; nums = nums[r] = temp;

    > >
    > > temp has no specified initial content. Start by thinking why you would

    > want
    > > to put an unknown value (which is not the same as random) in a specified
    > > address. My guesss: there is no good reason.
    > >

    >
    > That's not the problem. The temp variable is first used by assigning a
    > value to it. That's perfectly valid.
    >
    > The problem with his use of temp is that he put the second two assignments
    > into one statement, which is not what he wanted. I think that was

    probably
    > a mistake in typing (either when posting or coding, I can't tell). Using
    > better formatting, it should have been:
    >
    > temp = nums;
    > nums = nums[r];
    > nums[r] = temp;
    >
    > I suspect this is what was meant, but it got lost in the poor formatting.
    > Another good reason to put separate statements on separate lines! :)


    Here's the code, copied, pasted and reformatted:

    > > > for(i = 1; i < 50; i++)
    > > > {
    > > > r = (rand() % 49) + 1;
    > > > temp = nums;

    nums = nums[r] = temp;

    What is the value of nums on line 4? If it is not garbage, where did it
    get it's non-garbage value? Note especially that nums is an automatic
    variable.

    I made no attempt to fix the program, I merely pointed out the first error I
    noticed. I have a pet peeve of lab instructors that solve many, or most, of
    the students problems. The way one learns to program is to make mistakes
    and learn to locate and fix them.
    osmium, Sep 13, 2004
    #6
  7. Meyer von Landenhausen

    Howard Guest

    "osmium" <> wrote in message
    news:...
    > Howard writes:
    >
    > > "osmium" <> wrote in message

    >
    > > news:...
    > > > Meyer von Landenhausen writes:
    > > >
    > > > > hi please take a look at the source:
    > > > >
    > > > > #include <ctime>
    > > > > #include <iostream>
    > > > > using namespace std;
    > > > >
    > > > > int main()
    > > > > {
    > > > > char buffer[3];
    > > > > int i,r,temp;
    > > > > int nums[50];
    > > > >
    > > > >
    > > > > for(i = 1; i < 50; i++)
    > > > > {
    > > > > r = (rand() % 49) + 1;
    > > > > temp = nums; nums = nums[r] = temp;
    > > >
    > > > temp has no specified initial content. Start by thinking why you

    would
    > > want
    > > > to put an unknown value (which is not the same as random) in a

    specified
    > > > address. My guesss: there is no good reason.
    > > >

    > >
    > > That's not the problem. The temp variable is first used by assigning a
    > > value to it. That's perfectly valid.
    > >
    > > The problem with his use of temp is that he put the second two

    assignments
    > > into one statement, which is not what he wanted. I think that was

    > probably
    > > a mistake in typing (either when posting or coding, I can't tell).

    Using
    > > better formatting, it should have been:
    > >
    > > temp = nums;
    > > nums = nums[r];
    > > nums[r] = temp;
    > >
    > > I suspect this is what was meant, but it got lost in the poor

    formatting.
    > > Another good reason to put separate statements on separate lines! :)

    >
    > Here's the code, copied, pasted and reformatted:
    >
    > > > > for(i = 1; i < 50; i++)
    > > > > {
    > > > > r = (rand() % 49) + 1;
    > > > > temp = nums;

    > nums = nums[r] = temp;
    >
    > What is the value of nums on line 4? If it is not garbage, where did

    it
    > get it's non-garbage value? Note especially that nums is an automatic
    > variable.
    >



    Sorry, I misunderstood your inent. I assumed you were talking about the
    initial state of temp, *prior* to assigning anything to it. Also, by the
    blank lines in the original posted code, I just assumed that the array had
    been filled somewhere earlier , but that code wasn't shown because it wasn't
    deemed important to the question. (It would have helped if the OP had
    specified exactly what the actual output or error was, not just ask "what's
    wrong"?)

    -Howard
    Howard, Sep 13, 2004
    #7
    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. Kingsley Oteng

    Random Number Generator??

    Kingsley Oteng, Apr 27, 2004, in forum: VHDL
    Replies:
    11
    Views:
    52,073
    rahul.iyer
    Aug 9, 2010
  2. tshad

    Random number/letter generator

    tshad, May 5, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    9,548
    tshad
    May 6, 2005
  3. Wally
    Replies:
    1
    Views:
    2,772
    pvdg42
    Mar 20, 2006
  4. globalrev
    Replies:
    4
    Views:
    734
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,099
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page