473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to do pick random math operations in a C program?

9
Good day!
Im working on a math drill program where the user is asked to answer random math problems that involves the +, -, /, *, and % operators.
Now i dont know how to generate these randomly.
What commands and codes could I use?
So much thanks in advance!
Have a fruitful new year!
Dec 31 '09 #1
3 7594
Airslash
221 100+
generate a randon int value from 0 to 4 and depending on the value you perform the correct action using a switch statement

pseudo code:
Expand|Select|Wrap|Line Numbers
  1. // Generate a random int value in a range
  2. int val = GenerateRandomValue(0,4);
  3.  
  4. // call the correct action
  5. switch(val)
  6. {
  7.     case 0:
  8.         // do the + stuff
  9.     case 1:
  10.         // do the - stuff
  11.     case 2:
  12.         // do the * stuff
  13.     case 3:
  14.         // do the / stuff
  15.     case 4:
  16.         // do the % stuff
  17. }
  18.  
Dec 31 '09 #2
You can use rand

int k=rand()%5;

Use Randomize to get different values for each compilation
Jan 1 '10 #3
Banfa
9,065 Expert Mod 8TB
using

rand()%5

is a poor idea because the algorithm often used in the C standard library often produces results with poor randomness in the low order bits of its result.

Read C-Faq Question 13.16
Jan 1 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rob B | last post by:
Hello, I am just starting to learn Python and was writing a simple script on my machine (Mac OS X 10.3.4), but I can't seem to import the random module: #!/usr/bin/env python import random
3
by: Jules | last post by:
I've searched high and low for a 3 digit number generator that will pick at random a number between 000 and 999. The only things I have found were generators that pick numbers from 0 to 999. ...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
3
by: TaTonka | last post by:
hi! how can i manage it (html or jscript with css) that everytime a user loads or refreshes a page, the page has a new bgcolor. i want to put it in a single file, so that all my pages have the...
14
by: Debbie Lucier | last post by:
How would I generate a even random number each time? I can generate a random number each time but not an even one? Here is the code I use for the random number from 1-100. <script...
13
by: Jon Agiato | last post by:
Hello, I am sure this problem is easy to spot but I have been at this project all day and the frustration has finally overcome me. I am using this function in order to produce a standard normal...
15
by: Papajo | last post by:
Hi, This script will write a random number into a document write tag, I've been trying to get it to write into a input form box outside the javascript, any help is appreciated. Thanks Joe ...
12
by: Naya | last post by:
Hi. I am working on a math tutoring program which generates two random numbers (from 1 to 500) and asks the user to add them. How can I check to see whether or not they put in the correct total??...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.