Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi, how do I generate two random numbers, then place them in an equation in MVC. The user will then decide if the equation is correct by pressing a yes or a no button. The answer will be checked to see if its correct, then a new set of random numbers will be generated.

The closes thing that I found on the web is the link below, but there is a lot of extra stuff in it.
https://code.msdn.microsoft.com/Sample-of-a-simple-Captcha-f8c75c43

Thank you for your help.


This is what I have so far:

Model:
C#
public class Equation
{
public int a{get; set;}
public int b{get; set;}
public Awn = a+b;

var Rand = new Random((int)DateTime.Now.Ticks);
int a = rand.Next(10, 99);
int b = rand.Next(0, 9);
var equations = string.Format("{0} + {1} = {}", a, b);
}


Controller:
C#
public ActionResult Index()
{
return View();


View:
HTML
@using (Html.BeginForm())
{
@Html.LabelFor(model=>model.equations)
{
    <div class="row">
        <input type="submit" value="Yes"/>
        <input type="submit" value="No"/>
    </div>
}
Posted

Well a few suggestions:

Equation class should have a constructor that would contains the code (After the empty line in your example).

Property a and b setters should probably be private.

Awn should be a read-only property.

Not sur abot {} in the format string.

You also need code to receive the answer. Typically, this is done using parameters to the action and [HttpPost] attribute. Alternatively, validation might be done on client side if it make sense in your case.

Finally, you need to remember the "question" (or at least the anser) if you want to validate the answer on the server. One way to do it is to use hidden fields.
 
Share this answer
 
Let's say your equation is

X^2 + Y^2

Then you could have two variables (integers, probably) and set them each to a random number. (probably within certain limits - do you want to allow negative numbers, for example)

Then get the user's attempt into another variable (answer).

Then test if (answer == X*X Y*Y)
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900