Original Post
//srand(i) ; int ii =rand()%52; do the work? any better way?
#include<iostream.h>#include<ctime>//needed to seed time#include<conio.h>//uses getch functionint main(){ srand(time(0)); //seeds computers clock time to random generator //any number divided by 6 will return a remainder b/n 0 and 5 int die = (rand() % 6) + 1; //returns a number between 1 and 6 cout<<die<<endl; getch();//waits for user to press enter before closing}Quote:
should make a random number between 0 and 51, then add one to it (giving you the range of 1-52.
template<typename T> Random(const Type &Max, const Type &Min = (Type)0) { return ((Type)(((Type)rand() % (Max - Min)) + Min));}int i = Random(52, 1);Quote:
Original post by Winegums
int ii =(rand()%51) + 1;
should make a random number between 0 and 51, then add one to it (giving you the range of 1-52.
may not work, but quite sure it does.
Quote:
Original post by agi_shi
Make a function or macro for rand:
*** Source Snippet Removed ***
Then use it with:int i = Random(52, 1);
It just takes the range and makes a random number between that, and to make it instead from 0 to range to min to max you add min.
This is templated so it should work with doubles, floats, ints, etc.
Quote:
Original post by nobodynews Quote:
Original post by agi_shi
Make a function or macro for rand:
*** Source Snippet Removed ***
Then use it with:int i = Random(52, 1);
It just takes the range and makes a random number between that, and to make it instead from 0 to range to min to max you add min.
This is templated so it should work with doubles, floats, ints, etc.
I can't find anything to back me up on this, but I thought the mod operator only worked with integer types and not floats or doubles and therefore your code wouldn't work for non-integer types. Furhermore, you shouldn't really be using the mod operator for this purpose anyway as the distribution of numbers made this way will likely be skewed to the lower numbers.
Think about it this way: you have 10 lollipops and you want to divide them to 3 people. This means two people can get 3 and one person can get 4. By modding the random number you can cause similar problems.
This page looks good for information about random numbers and even shows code for the better version at the end:
http://www.daniweb.com/tutorials/tutorial1769.html
Quote:
Original post by Razorguts Quote:
Original post by Winegums
int ii =(rand()%51) + 1;
should make a random number between 0 and 51, then add one to it (giving you the range of 1-52.
may not work, but quite sure it does.
This shouldn't work, as the sample space from which the random number is drawn is still 0-51 (and thus the probabilities are skewed) - you'd never be able to randomly generate the number 52.
For example, I want a random number between 1 and 5. If I then add 3 to that number, it doesn't change my sample space to 1-8. And the #s 6, 7, and 8 would never be chosen.
int intInRange(const int begin, const int end){ if (begin == end) return begin; return ((rand() % (abs(end - first) + 1)) + first);}float floatInRange(const float begin, const float end){ if (begin == end) return begin; float generator = (float)(rand()/(float)RAND_MAX); return ((generator*(float)(fabs(end - first))) + first);}Quote:
Original post by superpig
I think you're misunderstanding how it's supposed to work. The rand()%51 expression (which should be rand()%52 as Shaku22 pointed out) gives you a random number in the range [0..50]. Adding one to the result effectively shifts the range - shifts, not expands - to [1..51]. Taking your example, adding 3 to a random number drawn from [1..5] shifts the sample space to [4..8], so 6, 7, and 8 will frequently be drawn. It's 1, 2, and 3 that wouldn't be.
Quote:
by superpig
...rand()%52... gives you a random number in the range [0..50].
Quote:
Original post by Razorguts Quote:
by superpig
...rand()%52... gives you a random number in the range [0..50].
How does the function rand()%52 yield a random number with an upper bound of 50?
Quote:
Original post by superpig
The rand()%51 expression(which should be rand()%52 as Shaku22 pointed out)gives you a random number in the range [0..50].
Quote:
Original post by Frequency
(int(rand()*RAND_MAX)%52)+1
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more