how to generate operators such as +,- randomly.......can we use random.range ...?

plz help…

1 Like

can you be a little more descriptive with your question. I’m not understanding what you’re asking for.

Random.range returns numbers, + and - are operators.

You could technically return random positive or negative numbers to simulate random addition or subtraction (plus a negative number is the same as minus).

Or you could have an enum that describes various operators, and then return a random enum value. And then switch over the enum and perform the appropriate operation.

But really, I can’t give a good suggestion until I know what you are actually trying to do.

sure its pretty easy to return one as a string to do with as you need, but you wont be able to return an operator specifically

string GetRandomOperator()
{
int ran = Random.Range(0,4);
switch(ran)
{
 case 0:
   return "+";

 case 1:
   return "-";

 case 2:
   return "*";

}

}

i want to generate + or - operator randomly…like braintuner2 game…?

can you give an example line, or example script to explain what your using this for. That may help explain a little. Most value that uses operators, can be adjusted/assigned from other bits of code.

(eg) // Untested example

var i = Random.Range(1 , 2);

if(i==1) {
  someValue += 4;
}
else if(i==2) {
 someValue -= 4;
}

let me give u an example…4+6=12

i hv already generated numbers randomly such as 4,6,12 as well as =…now i want to generate + or - symbol randomly…i.e

5 - 2 = 2 or 5 + 2 = 6 in such way and sequence of + and - symbols are not fixed…here is my code…

#pragma strict

var rand:int;

function Start()

{


rand = Random.Range(0, 10);

}

function OnGUI()
{


//var rand:int; 
//rand = Random.Range(0, 10);
//GUI.Label(Rect(20,20,100,30),rand.ToString());
GUI.Box (Rect (5,5,25,30), rand.ToString());
Debug.Log(rand);
GUI.Box (Rect (90,5,25,30),"=");

}

Well I’d say lambda’s and expression tree’s… but I think that’s beyond your level :stuck_out_tongue:

Create a ‘token’ - either using enum or class, then use switch or if statements to do the calculation the token represents.

using UnityEngine;
using System.Collections;

public class CalcTest : MonoBehaviour
{
    //untested
    public enum CalcToken { Add, Subtract }

    void Update()
    {
        if (!Input.GetKeyDown(KeyCode.Return)) return;
        var a = 5;
        var b = 10;
        var token = Random.Range(0, 2) == 0 ? CalcToken.Add : CalcToken.Subtract;
        var result = DoCalc(a, b, token);
        print(string.Format("a = {0}, b = {1}, token = {2}, result = {3}", a, b, token, result));
    }

    public float DoCalc(float a, float b, CalcToken token)
    {
        if (token == CalcToken.Add) return a + b;
        if (token == CalcToken.Subtract) return a - b;
        throw new System.NotImplementedException();
    }
}
1 Like

thnx alot to all of them for support…

I know this is a super old thread, but I kept coming back here when I was looking for the same thing. I think the question can be answered by returning either -1 or 1 then multiplying by the number you want to change the sign of.

This is what I have used

float sign = Mathf.Sign( (float)Random.Range (-1, 1));

float number = 6 + sign * 4;

This example will randomly return 10 or 2

The random range returns -1 or 0, and the Sign method will return -1 for negative and 1 for positive.

It doesn’t seem to be completely 50/50, but it worked for what I was looking for. If you are in the know, you could probably wrap this in an extension somewhere. I am not in the know. Also because I am a noob, I am not sure of efficiency.

4 Likes