Random #

hey guys i need to make a random # generator... and im having a problem... it keeps telling me that i need to return something.... but i've tried a bunch of dif. things... and im comin up short....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
 
int random (int min, int max) 
	{
    
 cout <<  rand() % 10 + 1 << endl;

		return;
}
int min = 1;
int max = 10;

void main() 
{ 

	cout << random (1,10) << endl;

	
}

this is the error its givin me

Error 1 error C2561: 'random' : function must return a value c:\users\www\documents\visual studio 2008\projects\rand0mz\rand0mz\randy numberz.cpp 11 Rand0mz
Last edited on
1) There's no point to those min / max global variable on line 13, 14

2) You don't srand at the start of your program.

3) Your 'random' function ignores the passed min/max parameters. It will always return [1..10] no matter what min/max you specify.

4) random should not be outputting to cout. Instead it should return a random number.

5) main should return an int, it should not be void.
6
7
8
9
10
11
12
int random (int min, int max)  // notice: not void
	{
    
 cout <<  rand() % 10 + 1 << endl;

		return; // what are you returning here?
}

idk... i got it working like this....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
 
int random (int min, int max) 
	{
	    srand(time(NULL));
			return  rand() % max + min;
}
		int min = 1;
			int max = 10;

void main() 
{ 

			cout << random (1,10) << endl;

			return;
}
Bzzzt.

Call srand() exactly once in your program. You are calling it every time random() is called.

Lines 12 and 13 are pointless.

Line 10 is wrong. Consider if min = 5 and max = 10. rand() % 10 yields [ 0... 9 ], plus 5
yields 5 ... 14.
It should be changed to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

srand(time(NULL)); 

int random (int min, int max) {
	return  rand() % (max + min);
}

void main() { 
	cout << random (1,10) << endl;
}
@Fsmv : still bad
1
2
3
4
//Returns a random number in the range [min, max)
int random (int min, int max) { 
    return rand()%(max - min) + min;
}

Last edited on
+ you can't have srand outside of a function like that

+ main should not be void
Just call srand as the first statement in main(). (which should be int not void)
I'm looking forward to the random generation changes in 0x, I'll say that much.
Dunno whether you care that much (many people don't care), but rand() % max is not really random for almost all values of max. To be precise: for all max except when they are a power of two (max = 1,2,4,8,16...)

This is because rand returns a random from 0 to RAND_MAX (which is usually 32767), so first of all it will never return anything bigger than RAND_MAX. But even for smaller values, you get some numbers much more often then other numbers. This problem is worse, the bigger "max" is.

Easiest to get a grasp of "why???" is: What if RAND_MAX would be 2 and you do rand()%2, then you get:

rand() returns 0 -> 0
rand() returns 1 -> 1
rand() returns 2 -> 0

so you get 0 twice as much as 1. Not particular random...


This a nice piece of code to try it out for yourself:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	int count = 0;
        int max = RAND_MAX/2;
        int number_of_tests = 10000000;

	for (int i = 0; i < number_of_tests; ++i)
		if ((rand() % max) == 0)
			count++;
	cout << "Actual: " << count << endl;
	cout << "Expected: " << (number_of_tests/max) << endl;
}


You count the numbers of zeroes. Should be one out of "max" times, right? Wrong! You'll be surprised.. ;-).


What is better? Well, I use this:

1
2
3
4
5
6
7
int better_rand(int max)
{
	int r,x = 1;
	while (x < max) x <<= 1;
	while ((r = rand() % x) > max);
	return r;
}


not the fastest, but doesn't have the issues with "rand() % max". If you want industrial strength random stuff in your game, forget about std::rand() at all...

And upcoming standard library will fix these problems anyway, by providing a fully fledged random number generator system.. ;-)

Ciao, Imi.

Edit: Just remembered: Beware, my better_rand returns values from 0 to max includive, whereas rand() % max does never result in "max". To get the same as rand()%max, change the second while to "...>= max..."
Last edited on
Output from my machine:


Actual: 0
Expected: 0


because RAND_MAX = 2^32 - 1 nowadays. 32767 comes from the 16-bit DOS days.

Nonetheless there is a slight bias involved in the formula:

 
rand() % ( max - min ) + min


which depends upon the values of min and max (actually
upon max - min).

To understand, let's say rand() only returned numbers between 0 and 9 inclusive.
Let's say max = 7 and min = 3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
rand()          result
   0               3
   1               4
   2               5
   3               6
   4               3
   5               4
   6               5
   7               6
   8               3
   9               4

P(3) = 3/10
P(4) = 3/10
P(5) = 2/10
P(6) = 2/10

because RAND_MAX = 2^32 - 1 nowadays. 32767 comes from the 16-bit DOS days.


*sniff*.. I am still living in good old 16-bit times.. ;-)

No, fun aside. RAND_MAX is 32767 on my latest VC.

So I conclude that my worrying is only really interesting if you are either using Windows and VC or really big max-values...

Ciao, Imi.
Sorry, typo on my part.

RAND_MAX = 2^31 - 1 (it is signed).
Topic archived. No new replies allowed.