[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

a math game for some kids

Posted on 2004-04-15
6
Medium Priority
?
439 Views
Last Modified: 2011-09-20
I am trying to write a math program for some kids I work with who are visually impaired and just need a little advice on a couple issues.  For one, I'm writing the program in C, and I'm trying to generate random numbers for the problems which are asked.  I seeded the generator with srand((unsigned)time(NULL)) at the beginning and am trying to exclude 0 from being generated as a possible number for a problem.  For example: firstint = rand()%maxint.  Maxint is defined according to how far the player has progressed in the game.  I want the generated number to be between 1 and maxint.  Any suggestions?  Thanks a lot in advance.
0
Comment
Question by:guyster104
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 10834614
firstint = 1+rand()%maxint;
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 10835054
if you use ozo way you could end up one over MaxInt so you could use

FirstInt = 1 + rand() % (MaxInt-1);
0
 
LVL 84

Expert Comment

by:ozo
ID: 10838008
1 + rand() % (MaxInt-1); will never return a value of MaxInt
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:Jase-Coder
ID: 10841226
why wouldnt it?

Say MaxInt = 5

I have took one away from MaxInt so,

the line of code could look like

rand() % 4

That means a number between 0 and 4 could be generated. The I have added one to the result so a number between 1 and 5 can be generated.

If your talking about the order of precedance guyster should use:

FirstInt = (rand() % (MaxInt-1)) + 1
0
 
LVL 1

Expert Comment

by:MortimerCat
ID: 10846887
Its been a few years since I have used C, so the loop construct is probably wrong, but the concept is there.

If you are not too sure about what Max & Min values you may end up with, you could always build in a safety net.

do
     FirstInt = 1 + rand() % (MaxInt-1);
until Firstint >=1 and FirstInt <=MaxInt
0
 

Author Comment

by:guyster104
ID: 10862540
Ozo's method worked well.  The program is well on its way to completion.  I would love to spice it up with some visual elements, such as dialogue boxes and a Windows style interface, but since I myself am visually impaired, don't know if I want to brave that one just yet.  Thanks to all for the suggestions.  Keep up the good work.

Guy
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question