cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
user name:
password:
Forgot your password?
please wait
try again
cancel
forgot your password?
sign up
log in
[Legacy version]
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
How can I generate a random number from
How can I generate a random number from 300 to 700?
Feb 20, 2013 at 3:41am
Feb 20, 2013 at 3:41am UTC
closed account (
4wRjE3v7
)
I've done codes before using this code:
secret = rand() % 100 + 1;
but how would I set it up so it only generates numbers from 300 to 700?
Thanks in advance!
Feb 20, 2013 at 3:46am
Feb 20, 2013 at 3:46am UTC
MrHutch
(1822)
rand()% 401 + 300;
Feb 20, 2013 at 3:46am
Feb 20, 2013 at 3:46am UTC
AbstractionAnon
(6954)
You want a range of 401 numbers.
secret=rand()%401;
Will give you a number from 0 to 400.
Now bias it by 300.
secret +=300;
Or in a single statement:
secret=rand()%401 + 300;
Topic archived. No new replies allowed.