cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Random function from 17 - 20
Random function from 17 - 20
Jun 15, 2012 at 5:56am UTC
anindyasd
(19)
hey guys i wanna ask you something. i wanna make a random function from 17 to 20.
but i don't know how to initialized the k value.
i just made it this way
1
2
3
4
5
6
for
(k=17; k<20; k++) { j = rand() % 20; a[j] = a[21]; a[k]=a[j]; }
what do you think?
Jun 15, 2012 at 6:02am UTC
Volatile Pulse
(1543)
rand() returns a random number, but I believe what you're looking for is:
j = ((rand() % 3 )+ 17);
This will assign j a value of 17-19.
Last edited on
Jun 15, 2012 at 6:14am UTC
Jun 15, 2012 at 6:14am UTC
anindyasd
(19)
eeerrr i mean 17-19 hehehe ^^v
Jun 15, 2012 at 6:14am UTC
Volatile Pulse
(1543)
Corrected.
Jun 15, 2012 at 7:26am UTC
MrHutch
(1822)
Make sure that at some point before this happens you've seeded from the system time (which should only happen once in your program).
Topic archived. No new replies allowed.