Random Number Generate

May 10, 2010 at 9:05pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <ctime>
using namespace std;

int main() {
	srand(static_cast<int>(time(0)));
	
	int iAPickFive[55]  = {};
	int randNumber      = 0;
	
	for(int i = 0; i < 55; i++)
		iAPickFive[i] = i + 1;
	
	for(int x = 0; x < 5; x++) {
		randNumber = (rand() % (54 + 1));
		cout << iAPickFive[randNumber] << " ";
		
		for (int i = randNumber; i < 55; i++)
			iAPickFive[i] = iAPickFive[i + 1];
		
		iAPickFive[55 - 1] = 0;
	}
	
	cout << endl;
	for(int x = 0; x < 55; x++)
		cout << iAPickFive[x] << " ";
	cout << endl;
	
	return 0;
}


Output

1
2
3
40 41 1 27 28
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 29 30 31 32 33 34 35 36 37 38 39 42 43 44 45 46 47 48 49 50 51 52 53 54 55 0 0 0 0 0
Press any key to continue . . .

I have to randomly output 5 numbers (1 - 55), without duplicating any numbers. I am terribly confused and need help. Is there a easier way to do this?

The number chosen is taken out and the array shifts to the left, leaving 0s.
May 10, 2010 at 10:43pm
There is a much easier way to do this, using stdlib :D aaaaand here we go....

1
2
3
4
5
6
7
8
9
10
#include <stdlib>

int main()
{
 for(i = 0; i < 5; i++)
{
    int x = rand() % 55 + 1;
}
return 0;
}


And that's it. rand() is quite a bit simpler to use and though it has its limitations it is perfect for what we are trying to do here. This is a link to the page on rand() : http://cplusplus.com/reference/clibrary/cstdlib/rand/
May 11, 2010 at 12:37am
Will this duplicate any numbers?
May 11, 2010 at 12:40am
It possibly could.
May 11, 2010 at 1:16am
If you want to check against duplicate numbers, then have a for loop that checks for repeated numbers. Okay, it will make your program run in factorial time, but hey... no duplicate numbers, and it's easy to implement.

Or, have an integer array that represents instead the time the value was returned rather than a value returned at a certain time.

-Albatross
Last edited on May 11, 2010 at 1:18am
May 11, 2010 at 1:17am
Use Archaics method to get the random numbers one at a time, put each one into an array, then scan through the array at each successive number to check for duplicates. Not sure if thats the accepted way to do it, but probably how I would do it given my total n00bness.
May 11, 2010 at 1:21am
I was going to delete my above post, and write everything from scratch, but... oops.

I actually recommend an integer array in a different sense than what we assume an... forget it, I'll just show you. How many values do you have? 56? Okay. Create a 56 element long array, each element initialized to minus one. When a certain random number is returned, return the iteration on which you got that random number into the array. If there is some value other than minus one, don't write anything in, and do not advance the counter (you'll want a while loop).

It's much more efficient than a search with a worst-case scenario performance of exactly the same as the other program (infinity), and a best-case scenario performance being linear.

-Albatross

Number of posts and counting:
1st-digit = 2nd-digit + 3rd-digit
Where: 2nd- and 3rd- digits equal their position, left to right.
Last edited on May 11, 2010 at 1:24am
May 11, 2010 at 7:46pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <ctime>
using namespace std;

int main() {
	srand(static_cast<int>(time(0)));
	
	bool duplicate = true;
	int iAPickFive[5]  = {};
	
	for(int i = 0; i < 5; i++)
		iAPickFive[i] = (rand() % 55) + 1;
	
	while(duplicate == true) {
		int dupCounter = 0;
		for(int i = 0; i < 5; i++) {
			for(int x = 0; x < 5; x++) {
				if(iAPickFive[i] == iAPickFive[x] && x != i) {
					dupCounter++;
					iAPickFive[x] =  (rand() % 55) + 1;
				}
			}
		}
		if(dupCounter == 0)
			duplicate = false;
	}
	
	cout << "Your pick five numbers are: ";
	for(int i = 0; i < 5; i++)
		cout << iAPickFive[i] << " ";
	cout << endl;
	
	return 0;
}

Would this be a better way? Can anyone find any bugs?
Last edited on May 11, 2010 at 7:58pm
May 11, 2010 at 8:12pm
Looks like I had the same idea as Albatros:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
	srand(time(0));

	bool done[55];
	for(size_t i = 0; i < 5; ++i)
	{
		done[i] = false;
	}

	for(size_t i = 0; i < 5;)
	{
		int n = (rand() % 55);
		if(!done[n])
		{
			std::cout << n + 1 << ' ';
			done[n] = true;
			++i;
		}
	}
}
Last edited on May 11, 2010 at 8:13pm
May 11, 2010 at 9:43pm
That's clever piece of code. It makes sense after seeing it.

However, in this piece of code are you just assigning the first four elements false?
1
2
3
for(size_t i = 0; i < 5; ++i) {
	done[i] = false;
}
Last edited on May 11, 2010 at 9:56pm
May 12, 2010 at 12:05am
I don't understand that for loop, is it suppose to assign false to all the elements?
May 12, 2010 at 8:36am
Yes, you are right :o)

It should be:
1
2
3
for(size_t i = 0; i < 55; ++i) {
	done[i] = false;
}
May 12, 2010 at 10:31am
You can also use STL find algorithm here.
1
2
3
4
5
6
7
8
9
10
11
     int picked[5] , i  ;    
    srand(time(NULL));
    for(  i = 0  ; i < 5 ;  )
    {
         int temp = rand()%55  ;
         if( find( picked , picked+i , temp ) == picked + i )
         {
            picked[i] = temp ;            
            cout << picked[i++] << " " ;
         }
    }
May 12, 2010 at 9:39pm
How would I go about adding commas between each number outputted?
1
2
3
4
5
6
7
8
9
std::cout << "Your Pick 5 numbers are: ";
	for(int i = 0; i < 5;) {
		int n = (rand() % 55);
		if(!done[n]) {
			std::cout << n + 1 << ", ";
			done[n] = true;
			i++;
		}
	}


Output:
Your Pick 5 numbers are: 4, 2, 3, 41, 9,
Your Power Ball number is: 30


Last edited on May 12, 2010 at 9:41pm
May 17, 2010 at 8:49pm
Any help on how to do this?
May 18, 2010 at 7:24pm
ummm, aren't there commas in there now?
May 19, 2010 at 3:27am
Yeah, but there's a extra comma at the end.
May 19, 2010 at 5:34am
Make an exception for the last loop, that if i = 4, don't print a comma?

-Albatross
May 19, 2010 at 7:29am
I tend to do that like this:

1
2
3
4
5
6
	const char* sep = "";
	for(size_t val = 0; val < 5; ++val)
	{
		std::cout << sep << val;
		sep = ", ";
	}
May 19, 2010 at 12:07pm
Maybe try:
std::cout <<( val? ", " : "" )<< val;
Last edited on May 19, 2010 at 12:08pm
Topic archived. No new replies allowed.