random number generator

  • Thread starter Meyer von Landenhausen
  • Start date
M

Meyer von Landenhausen

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
 
O

osmium

Meyer said:
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
 
J

John Harrison

Meyer von Landenhausen said:
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
 
R

rossum

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
 
H

Howard

osmium said:
Meyer said:
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
 
O

osmium

Howard said:
Meyer said:
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.
 
H

Howard

osmium said:
Howard said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top