AnthIste 0 Newbie Poster

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

frtzfoxy -1 Newbie Poster

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!!!
*/
NathanOliver commented: 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
Lucaci Andrew commented: As NathanOliver said, plus he wrote it 8 years ago. /sight -1
deceptikon 1,790 Code Sniper Administrator Featured Poster

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. ;)

Sendy Hipo -3 Junior Poster in Training

deceptikon, thanks for the link ! now i know how to use rand() correctly :D

azeem abbas 0 Newbie Poster

you can do it easily just by only rand function
this is link which clearly explains how to generate random number with example.it will really help you.
http://softwares-mart.blogspot.com/2012/12/how-to-generate-random-number-in-c.html

shadia_1 0 Newbie Poster

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

BugsRule 0 Newbie Poster

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.

Megan_3 0 Newbie Poster

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

NathanOliver 429 Veteran Poster Featured Poster

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');
prime09 0 Newbie Poster

Thank you Bob. This is the first time i figure out a random number.

Emmanuel_4 0 Newbie Poster

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

Zain_4 0 Newbie Poster

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

David W 131 Practically a Posting Shark

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.

Cheese_1 0 Newbie Poster

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)

Reverend Jim 3,431 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

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.

Ty_1 0 Newbie Poster

I have a question to ask. It is that i trying to make a program that using do-while loop but the random number seems like doesn't change after the first one. It keep continue appears the same number. How can I make it random everytime the loop. Thanks.

JamesCherrill 4,301 Most Valuable Poster Moderator Featured Poster

Ty_1

  1. Do not hijack old topics for your new question. Start you own new topic.
  2. If you do not post your code then nobody can tell you what you are doing wrong!
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.