I know how to make random numbers. and How to make random numbers in a specific range. BUT!!! How can you display 'x' random numbers in a given range and make it so the output display lists the numbers in groups a 'y'. For example: I want to display 25 random numbers between 1-100 in groups of 5 numbers per line. Of course the user could change those values. the values of 'x' and 'y' are decided by the user. any help of hints would be great.

You could use 2 nested loops:

for (int x = 0; x < user_x; x++)
{
    for (int y = 0; x < user_y; y++)
    {
         // do stuff
    }
    std::cout << '\n'; // next line
}

Or you could use one loop and use the remainder operand

for (int i = 0; i < user_x * user_y; i++) {
    // do stuff
    if (i % user_x == 0) std::cout << '\n';
}

That should get you started

I know how to make random numbers. and How to make random numbers in a specific range. BUT!!! How can you display 'x' random numbers in a given range and make it so the output display lists the numbers in groups a 'y'. For example: I want to display 25 random numbers between 1-100 in groups of 5 numbers per line. Of course the user could change those values. the values of 'x' and 'y' are decided by the user. any help of hints would be great.

So 'x' is the number of numbers per line, 'y' is the number of lines? Is there a 'z' involved, as in the total number of groups?

xxxx
xxxx
xxxx

xxxx
xxxx
xxxx

x = 4, y = 3, z = 2 above?

Is there a 'z' involved, as in the total number of groups?

z can be calculated using x and y, but having an actual z makes it harder to handle an x that isn't perfectly divisible by y.

commented: Bork Bork Bork +12

sorry I forgot to add the groups need to be on the same line separated by a blank line. would it be sometime similar? I keep getting errors. thanks

So 'x' is the number of numbers per line, 'y' is the number of lines? Is there a 'z' involved, as in the total number of groups?

xxxx
xxxx
xxxx

xxxx
xxxx
xxxx

x = 4, y = 3, z = 2 above?

If you are getting errors, then you have some code, right? Somebody can help you fix them if you show the code and say what the errors are.

actually i figured out the error. it had nothing to do with the grouping sorry. still don't know what to do.

If you are getting errors, then you have some code, right? Somebody can help you fix them if you show the code and say what the errors are.

sorry I forgot to add the groups need to be on the same line separated by a blank line

That's idiotic I think you want to say blank space.
Hey you really cant expect us to give every detail.You need to work yourself.If you need the groups to be on separate lines you used to insert a "\n" or an endl in cout now when you want them to be separated by a space for the same code insert " " (space) rather than "\n".

Thats what Tom Gun said,you have your code right post it in.

what's idiotic is that you clearly didn't understand what I wanted and yet decided to post something anyway. I don't expect you to do the work, I have plent to do without your help. So don't reply if you have nothing useful to say seriously.

I want to have groups of numbers on the same line decided by the user.

x,x,x,x,x
x,x,x,x,x
x,x,x,x,x

you don't need my code for that, because I don't know how to display that. so if you don't know then don't post.

That's idiotic I think you want to say blank space.
Hey you really cant expect us to give every detail.You need to work yourself.If you need the groups to be on separate lines you used to insert a "\n" or an endl in cout now when you want them to be separated by a space for the same code insert " " (space) rather than "\n".

Thats what Tom Gun said,you have your code right post it in.

You had x items y count per line!

Please write your code and post for our review!

while x > 0
       n = min( x, y )
       x -= n

      while (n)
            print the rnd #
      end
      print the end of line.
end

I don't expect you to do the work

I mean no offense, but a lot of people around here are going to stop helping you if you don't show your code soon. It's easy to post code and prove that you are trying. It's not so easy to convince everyone that you're not looking for a free ride without proof.

you don't need my code for that, because I don't know how to display that.

niek_e's examples do what you want. If you can't get them to work, show your code and someone can help you fix the problem.

@zeus1216gw:

No need to flame up ok.Read your post (#5) k.It reads as below :

sorry I forgot to add the groups need to be on the same line separated by a blank line. would it be sometime similar? I keep getting errors. thanks

I called the phrase idiotic not you.And we before helping have every right to ask you to show your effort(Sometimes the codes).And that was asked to help you out.So keep your calm and read what people have posted and what you have posted before flaming !!!

Sorry about getting fired up. this is what i have so far. i can display the numbers all on the same line. but if the user wanted something like 40 numbers...the list needs to broken up into blocks of 5 numbers per line. Obviously that would change as user inputed different things.

int main()
{
  double randValue;
  int a,b,i, n, entry, y, count;
  char again = 'y';

  while (again == 'y')
  {
  cout << "Please enter the number of random numbers you want(1-100): "<<endl;
  cin >> n;	 
  cout << endl;
  
	  
 if (n <= 100)
 {srand(time(NULL));  // this generates the first "seed" value
  cout << "Please enter the range you want (lowest to highest) : " <<endl;
  cin >> a;
  cin >> b;
  
 

  if (a<b)
  {

	  
  for (i = 1; i <= n; i++)
  {
    randValue = a + rand() % (b+1-a);
    cout << randValue << "   ";
	
	
  }
  
  
  return 0;}

  else 
  { cout << "Enter the range from lowest to highest only!" <<endl;
    cout << "Do you wan to continue? y or n?" <<endl;
	cin >> again;}
 
  }

 else {

	 cout << "Enter a value between 1-250 only!" << endl;
	 cout << "Do you want to continue? y or n??" << endl;
	 cin >> again;
	
 
 }}

	  
		  

  }

So 'x' is the number of numbers per line, 'y' is the number of lines? Is there a 'z' involved, as in the total number of groups?

xxxx
xxxx
xxxx

xxxx
xxxx
xxxx

x = 4, y = 3, z = 2 above?

z can be calculated using x and y, but having an actual z makes it harder to handle an x that isn't perfectly divisible by y.

Not if you define z as I did above. Are you defining z as the total number of numbers? I wasn't, but maybe the OP was.

I want to have groups of numbers on the same line decided by the user.

x,x,x,x,x
x,x,x,x,x
x,x,x,x,x

I imagine this thread is quickly going to devolve into something totally unrelated to the problem, but what the hell, I'll try to clarify anyway. How many inputs are you looking to have and what are they? 5 and 3 for the above pattern. 5, 3, and 1 if it's following what I did earlier(1 group of 3 lines, with 5 per line)? 5 and 15 (5 per line, 15 total number)?

[EDIT]
Missed a post by the OP while I was editing mine. I'll look at it later. It may have answered my question.
[/EDIT]

I shuffled your code around so errors are handled immediately. That you're more user friendly. that you handle Uppercase Y as well as lower case Y. That it handles the column count, etc.
Also changed your Floating-Point to integer!

Feel free to pick and choose!

int main()
{
	int randValue;
	int a,b,i, n, entry, y, count, column, t;
	char again = 'y';

	column = 5;
	srand(time(NULL));  // this generates the first "seed" value

	while ( tolower(again) == 'y')
	{
		cout << "Please enter the number of random numbers you want(1-100): "<<endl;
		cin >> n;	 
		cout << endl;

			// Handle out of range early so can see in context to error!
		if (n > 100)
		{
			cout << "Enter a value between 1-100 only!" << endl;
			cout << "Do you want to continue? y or n??" << endl;
			cin >> again;
			continue;
		}

		cout << "Please enter the range you want (lowest to highest) : " <<endl;
		cin >> a;
		cin >> b;

		if (a > b)			// Being user friendly, shuffle into low to high order
		{
			t = a;
			a = b;
			b = t;
		}

		while ( n )
		{
			t = (column < n) ? column : n;
			n -= t;
			while (t--)
			{
				randValue = a + rand() % (b+1-a);
				cout << randValue << "   ";
			}
			cout << eol;
		}
	}

	return 0;
}
int main()
{
double randValue;
int a,b,i, n, entry, y, count;
char again = 'y';

while (again == 'y')
{
  cout<<"Please enter the number of random numbers you want(1-100):"<<endl;
  cin>>n;
  cout<<endl;


  if (n <= 100)
  {
     srand(time(NULL));  // this generates the first "seed" value
     cout << "Please enter the range you want (lowest to highest) : " <<endl;
     cin >> a;
     cin >> b;
     cout<< "Please enter the number of elements you want in a group "<<endl;
     int cnt;
     cin >> cnt;

  if(a<b)
  {
     for (i = 1; i <= n; i++)
     {
         randValue = a + rand() % (b+1-a);
         cout << randValue << "  ";
         if(i%cnt==0) cout<<endl;
     }
     return 0;
  }
  else
  {
    cout << "Enter the range from lowest to highest only!" <<endl;
    cout << "Do you wan to continue? y or n?" <<endl;
    cin >> again;
  }
}

else
{
     cout << "Enter a value between 1-250 only!" << endl;
     cout << "Do you want to continue? y or n??" << endl;
     cin >> again;
}
}
}

The additions made in red are the only things you need to change to get your desired output.

Not if you define z as I did above. Are you defining z as the total number of numbers? I wasn't, but maybe the OP was.

You're the only one who started talking about z. The OP only wanted x and y. x is the total count of random numbers, y is the count of numbers in each group. The number of groups is calculated by x/y.

I'm defining z as the number of groups, just as you did. But all it does is make the logic more complex. You have to validate z, then you have to add extra tests to make sure that partial groups don't get lost, or that too many numbers don't get printed because you rely on z. That extra work is pointless when simple logic using x and y does the job:

int i = 0,
    j = 0;

while (true)
{
    cout << lo + rand() % (hi - lo);

    if (++i == x)
    {
        cout << '\n';
        break;
    }
    else if (++j == y)
    {
        cout << '\n';
        j = 0;
    }
    else cout << ',';
}

Thanks a lot. that makes sense now that I look at it. correct me if I'm wrong here.

i%count == 0 endl... translates into:

if the remainder of i divided by the count is zero then skip a line. just trying to re-enforce what i've learned here.

You're the only one who started talking about z. The OP only wanted x and y. x is the total count of random numbers, y is the count of numbers in each group. The number of groups is calculated by x/y.

I'm defining z as the number of groups, just as you did. But all it does is make the logic more complex. You have to validate z, then you have to add extra tests to make sure that partial groups don't get lost, or that too many numbers don't get printed because you rely on z. That extra work is pointless when simple logic using x and y does the job:

I was defining the goal like this:

xxxx
xxxx
xxxx

xxxx
xxxx
xxxx

I was defining x as the number of randoms per line, not the total number of randoms. A "group" is 12 here. There are 2 groups of 12, so 24 in all. What I wasn't clear on is what the word "group" meant to the OP. It sounds like he DOES NOT intend to do what I did above, thus we were defining "group" differently. But I wasn't sure originally and it wasn't clear what exactly was required and what the input was.

great thanks a lot. one other thing to make sure I'm not gaffing this up. but I can loop the random numbers into an array to do other math functions later right?

i don't think there really is any other way to save an ever changing list of numbers...could be wrong. thanks for the help though.

your way is still cool though. i'll have to remember that process for the future.
thanks man!

I was defining the goal like this:

xxxx
xxxx
xxxx

xxxx
xxxx
xxxx

I was defining x as the number of randoms per line, not the total number of randoms. A "group" is 12 here. There are 2 groups of 12, so 24 in all. What I wasn't clear on is what the word "group" meant to the OP. It sounds like he DOES NOT intend to do what I did above, thus we were defining "group" differently. But I wasn't sure originally and it wasn't clear what exactly was required and what the input was.

Thanks a lot. that makes sense now that I look at it. correct me if I'm wrong here.

i%count == 0 endl... translates into:

if the remainder of i divided by the count is zero then skip a line. just trying to re-enforce what i've learned here.

Ya if remainder is zero then "skip the line" or "print the next number from next line" or "go to next line" which ever way you wanna say.By doing that you get out of trouble of dividing the numbers into groups(all the multiple of n issues) and will get a perfectly formatted output.

And ya once you are done mark the thread solved.If not satisfied you can continue posting in your queries.

one other thing to make sure I'm not gaffing this up. but I can loop the random numbers into an array to do other math functions later right?

Ya you can push those random numbers generated into an array for future use.But I don't see any sense in doing that because if you want random numbers in future then you would generate them then and there itself,no need to pre compute them and store them.However if you want to do so and your code needs it you can do so.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.