C++/plz help

Advertisement


Question
hi
can you give me a code wrote with rnd function for choosing a number between 1-10?
i really get confused with this function, and really need your help
thanx
Bita

Answer
Hi Bita,

It would help others if your subject contained a hint of what you wanted help with.

It's rand() - maybe you mistyped.

rand() % N gives you a number from 0 to N.

For general purposes:

rand() % N + M

gives you a number from M to N+M.

Solving this in reverse for a random number from A to B:

rand() % (B-A) + A

It follows: rand() % 9 + 1 gives a number from 1 to 10

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.