0

and let's say i have a range from 2 to N, what should i do to get the output only from 2 to N and not 0 to N?because what's given in the previous thread is a function to choose from 0 to N.
thank you :D

Say you want a random number from 2 to n where n is 15, you would do the following:

int n = 15;
int random_int;

random_int = (rand()%n-2)+2;

lets look at the min/max scenarios.

MIN:
Should be 2 (right?)
so you random 0. But you said +2 at the end. so you get 2. which is minimum.

MAX:
should be 15. But max of (15-2) is 13. But you added 2. So its 15.

Problem solved

-3

OKAY YOU NOOBS!!!!!
HERE IS A MUCH SIMPLER THING TO DO!!!!

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int randomNumber = rand() % 100 + 1; // This would randomize the number from 1-100
cout << "The Random Number Is: " << randomNumber << endl;
system("pause");
return 0;
}
/*
If you want to randomize from 1-10
then put: 10 + 1
If you want to randomize from 1-9999
then put: 9999 + 1
SIMPLE!!!
*/
Votes + Comments
As NathanOliver said, plus he wrote it 8 years ago. /sight
You dont need to ressurect a dead thread to add nothing new. check out the first post and you will se he has this same bit of code except its for 1-10
0

OKAY YOU NOOBS!!!!!
HERE IS A MUCH SIMPLER THING TO DO!!!!

It's also subtly broken if you care about uniform distribution. Simple isn't always better...noob. ;)

Edited by deceptikon

0

Thanks for the feedback about the font size on my web site. I wasn't aware that anyone had a problem with it till now. I'll look into it

0

Thanks, Bob! Expecially love the last line, the Conclusion. rand() is great! But for small programs. I use some complicated external library and rand() is not working.

0

Hello (if anyone is here),
I was looking through the previous discussion messages and saw someone had put #include <conio> to use the getch() but I'm using DEV-C++ and it says there is no such file.. Is that something that is in Visual C++? If I'm trying to loop the program if "y" for y/n choice, how would I do that using the while fucntion?
Thank you, Megan

Edited by Megan_3

0

using standard code you could do the following

char choice;
do
{
    // your code goes here
    std::cout << "Do you want to go again (y/n): ";
    std::cin.get(choice);
} while (::tolower(choice) == 'y');
0

Bob!! Thank you bro! The tutorial was really helpful! learned alot from this.
--Amateur

0

Yo!
can you show us how to print random numbers between -1 to +1?
thanks

0

Hey @ Zain_4 ...

Do you realize that it is not proper etiquette to hijack a thread?

If you wish to ask a new question, please start a new thread ...

and if you need programming help with an homework question ...

you will NEED TO FIRSTLY show us the code that you have tried so far ...

so that we may see how to best help you.

Edited by David W

0

how would i make something akin to a dice roll game. for example: the player would roll and it would generate a random number (perhaps from (1-60) and then the program would generate a random number for itself, acting as a simple AI (im really new to c++ and i want to learn this stuff)

1

Well, first I would learn how to program in C++. What I would not do is ask someone else to do it without showing that I had put some effort into doing it myself first.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.