Random Number
-
Hi! I've to select a number radomly from 1 t0 6. What is the C++ code for this?
Since (from rand() as cplusplus.com[^]):
Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.
An alternative is:
int n = 1 + 6.0 * rand() / (RAND_MAX + 1.0);
Veni, vidi, vici.
-
Since (from rand() as cplusplus.com[^]):
Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.
An alternative is:
int n = 1 + 6.0 * rand() / (RAND_MAX + 1.0);
Veni, vidi, vici.
CPallini wrote:
Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.
That's wrong. I startet a small programm generating one billion random number for each of the two solutions repeatly. The are no difference and the reason is obvious. Of course smaller numbers are more likely, but it doesn't matter how big a number is when you use modulo as it depends on divisibility. And there is no difference for small or large numbers.
------------------------------ Author of Primary ROleplaying SysTem How do I take my coffee? Black as midnight on a moonless night. War doesn't determine who's right. War determines who's left.
-
Hi! I've to select a number radomly from 1 t0 6. What is the C++ code for this?
This should do it.
int randomizedValueInRange = (rand()%(maxRange-minRange+1))+minRange;
You talk about Being HUMAN. I have it in my name AnsHUMAN
-
CPallini wrote:
Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.
That's wrong. I startet a small programm generating one billion random number for each of the two solutions repeatly. The are no difference and the reason is obvious. Of course smaller numbers are more likely, but it doesn't matter how big a number is when you use modulo as it depends on divisibility. And there is no difference for small or large numbers.
------------------------------ Author of Primary ROleplaying SysTem How do I take my coffee? Black as midnight on a moonless night. War doesn't determine who's right. War determines who's left.
-
It depends on the goodness of the pseudo random generator , of course (hint: it is not the reminder function that is 'broken'). The test depends also on the periodicity of the random generator. How did you perform the test?
Veni, vidi, vici.
CPallini wrote:
How did you perform the test?
I had just two arrays of long integer, filled with random numbers. Then I calculated the difference between the two, printed them and repeated the test, adding the differences. Of course there are differences but they are neglectable and there is no scheme that they will raise with higher numbers.
------------------------------ Author of Primary ROleplaying SysTem How do I take my coffee? Black as midnight on a moonless night. War doesn't determine who's right. War determines who's left.
-
Hi! I've to select a number radomly from 1 t0 6. What is the C++ code for this?
The code is as follows :
int random_number = (rand() % 6) + 1;
Example of use:#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std ;int main(void)
{ //seed
srand(static_cast<unsigned>(time(0)));cout<<" \\nDICE GAME\\n"; //Generate a random number between 1 and 6 int random\_number = (rand() % 6) + 1; cout<<" \\nYou threw a \["<<random\_number <<"\]\\n\\n";
return 0;
} -
Hi! I've to select a number radomly from 1 t0 6. What is the C++ code for this?
-
Typically these requests for code result in a lot of sarcastic responses, and being down voted into oblivion what makes one different?
Where did you see a sarcastic reply in this thread?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Where did you see a sarcastic reply in this thread?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
I didn't, that's why I'm curious, no 'google it' posts, no links to 'how to ask a question' specifically the part about not doing others' homework, etc... So I asked what makes this post unique?