# random number generator

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

1. ### Meyer von LandenhausenGuest

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

2. ### osmiumGuest

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

3. ### John HarrisonGuest

"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
4. ### rossumGuest

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
5. ### HowardGuest

"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
6. ### osmiumGuest

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
7. ### HowardGuest

"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