Search Unity

Junior programmer: Create with code 1. ¿Trick quiz question?

Discussion in 'Community Learning & Teaching' started by awsapps, Jun 19, 2021.

  1. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    74
    Hello,

    In the following question, you've to choose the posible values of randomFloat and randomInt:

    upload_2021-6-19_20-44-47.png

    Shouldn't the first line include the character "f"?
    Code (CSharp):
    1. float randomFloat = Random.Range(0f,100f);
    (Junior programmer: Create with code 1 -> unit 2 -> Quiz)
     

    Attached Files:

  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    It's fine. It's the same idea as
    float n=0;
    being legal. The system automatically converts the int 0 into the float 0.0f (which is called an implicit cast). It's there since adding a point-0f is safe, and having to type point-zero all the time is annoying.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,443
    result does get converted, but using int params would only give random ints (not floats),
    and big difference on the overloads: using floats its maxInclusive , using int its maxExclusive:
    https://docs.unity3d.com/ScriptReference/Random.Range.html

    so it would seem like its missing the f's
     
    awsapps likes this.
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    The Q works both ways. Maybe it's about how int-random can't take the highest value, and float can. But it could also be about how overload resolution doesn't check the return value, so (0,100) uses the same int-random each time. I've asked Q's like
    float n = 10/4;
    (which is 2.0f, since 10/4 is 2). Of course, I've asked them _after_ going over the process in detail.

    But, yeah, it's probably missing at least 1 dot-f (and should have both). It's probably just after teaching about random, and the students haven't seen stuff like 2.3+4/5-1.1 yet. In that case the answer would be #1. In my version, none are correct, which could be fine since it could be "select any or all of these could happen".