Asked — Edited
Resolved Resolved by DJ Sures!

Generate A Random Colour

Good Evening,

I am trying to generate a random colour from four predefined colours. The pre defined colours are Red,Blue,Green,Yellow. Please take a look at what I written so far.

:COLOR $1 = "RED" $2 = "GREEN" $3 = "BLUE" $4 = "YELLOW" $COLOUR = GETRANDOM($1,$4) say($COLOUR)

I know why the error I am getting is because the GetRandom is looking for a numeric value. I do not know if the script is capable of what I am trying to accomplish.


ARC Pro

Upgrade to ARC Pro

Elevate your robot's capabilities to the next level with Synthiam ARC Pro, unlocking a world of possibilities in robot programming.

PRO
Synthiam
#1  

EZ-Script is very powerful and can do way more than what you're asking:)

This is one way to do it...


DefineArray($colors, 3)

$colors[0] = "Red"
$colors[1] = "Green"
$colors[2] = "Blue"

$randomColor = getRandom(0, 3)

say("The color is " + $colors[$randomColor])

This is an easier way to do it...


$randomColor = GetRandom(0, 2)

if ($randomColor = 0)
$color = "Red"
elseif ($randomColor = 1)
$color = "Green"
else
$color = "Blue"
endif

say("The color is " + $color)

United Kingdom
#2  

When I was building Jarvis I had him choose his own colour.


# Colour chooser script for Jarvis self design

# Use voice recognition to ask Jarvis what colour he wants to be
# Use random numbers to choose a colour
# Run the choices until the same is picked twice consecutivly

# Colour options
$black = 0
$brown = 0
$red = 0
$orange = 0
$yellow = 0
$green = 0
$blue = 0
$violet = 0
$grey = 0
$white = 0
$silver = 0
$gold = 0


SayWait("Hummmm let me think")
Sleep(500)

$choice1 = GetRandom(0,11)
Goto(pick1)
SayWait("I quite like $colour")

:loop
$choice2 = GetRandom(0,11)
IF ($choice1 = $choice2)
  Goto(done)
ELSE 
  Sleep(300)
  Goto(pick2)
  IF ($black>3 OR $brown>2 OR $red>2 OR $orange>2 OR $yellow>2 OR $green>2 OR $blue>2 OR $violet>2 OR $grey>2 OR $white>2 OR $gold>2 OR $silver>2)
    goto(done)
  ELSE 
    $phrase = GetRandom(0,3)
    IF ($phrase = 0)
      SayWait("but I also like $colour")
    ELSEIF ($phrase = 1)
      SayWait("although $colour is nice")
    ELSEIF ($phrase = 2)
      SayWait("but theres something to be said about $colour")
    ELSEIF ($phrase = 3)
      SayWait("but $colour is cool")
    ENDIF 
  ENDIF 
ENDIF 
$choice1 = GetRandom(0,11)
IF ($choice1 = $choice2)
  Goto(done)
ELSE 
  Sleep(300)
  Goto(pick1)
  IF ($black>2 OR $brown>2 OR $red>2 OR $orange>2 OR $yellow>2 OR $green>2 OR $blue>2 OR $violet>2 OR $grey>2 OR $white>2 OR $gold>2 OR $silver>2)
    goto(done)
  ELSE 
    $phrase = GetRandom(0,3)
    IF ($phrase = 0)
      SayWait("but I also like $colour")
    ELSEIF ($phrase = 1)
      SayWait("although $colour is nice")
    ELSEIF ($phrase = 2)
      SayWait("but theres something to be said about $colour")
    ELSEIF ($phrase = 3)
      SayWait("but $colour is cool")
    ENDIF 
  ENDIF 
ENDIF 
Goto(loop)

:pick1
IF ($choice1 = 0)
  $colour="black"
  $black++
ELSEIF ($choice1 = 1)
  $colour="brown"
  $brown++
ELSEIF ($choice1 = 2)
  $colour="red"
  $red++
ELSEIF ($choice1 = 3)
  $colour="orange"
  $orange++
ELSEIF ($choice1 = 4)
  $colour="yellow"
  $yellow++
ELSEIF ($choice1 = 5)
  $colour="green"
  $green++
ELSEIF ($choice1 = 6)
  $colour="blue"
  $blue++
ELSEIF ($choice1 = 7)
  $colour="violet"
  $violet++
ELSEIF ($choice1 = 8)
  $colour="grey"
  $grey++
ELSEIF ($choice1 = 9)
  $colour="white"
  $white++
ELSEIF ($choice1 = 10)
  $colour="silver"
  $silver++
ELSEIF ($choice1 = 11)
  $colour="gold"
  $gold++
ENDIF 
Return()

:pick2
IF ($choice2 = 0)
  $colour="black"
  $black++
ELSEIF ($choice2 = 1)
  $colour="brown"
  $brown++
ELSEIF ($choice2 = 2)
  $colour="red"
  $red++
ELSEIF ($choice2 = 3)
  $colour="orange"
  $orange++
ELSEIF ($choice2 = 4)
  $colour="yellow"
  $yellow++
ELSEIF ($choice2 = 5)
  $colour="green"
  $green++
ELSEIF ($choice2 = 6)
  $colour="blue"
  $blue++
ELSEIF ($choice2 = 7)
  $colour="violet"
  $violet++
ELSEIF ($choice2 = 8)
  $colour="grey"
  $grey++
ELSEIF ($choice2 = 9)
  $colour="white"
  $white++
ELSEIF ($choice2 = 10)
  $colour="silver"
  $silver++
ELSEIF ($choice2 = 11)
  $colour="gold"
  $gold++
ENDIF 
Return()

:done
Sleep(1000)
SayWait("Actually I really like $colour")
Halt()

This one is a lot more involved than DJ's examples in the fact it will pick a colour, then change it's mind, pick another one etc. until the same colour is picked multiple times. It also gives voice feedback on each pick.

Script is rather old now and may have some errors in due to syntax changing (probably the SayWait() commands) but the idea is there to build off of. It's also a very rough script, I would probably have written it differently today as there are many ways I can see without too much probing that I could have made it a lot less code.

#3  

@DJ: Thank You! As the end user, how could I have known to use the DefineArray command?

@Rich: Thank You also for your help.

May I use the scripts posted in this thread in my project?

#4  

@Wayne... Using an array is a very common programming technique..... Also, there is lots of example code within ARC to show you how to do all this stuff....

United Kingdom
#5  

You don't need to use arrays, my example doesn't since I wrote that before I played with arrays. Arrays are a neater method though.

To answer "how would I know to use..." this is, as Richard mentioned, common programming technique. You know by learning, either in a class room, from tutorials and books or from examples.