Write a program that randomly chooses between three different colors for display
ID: 3532820 • Letter: W
Question
Write a program that randomly chooses between three different colors for displaying text on the screen. Use a loop to
display twenty lines of text, each with a randomly chosen color. The probabilities for each color are to be as follows:
white = 30%, blue = 10%, green = 60%. Hint: generate a random integer between 0 and 9. If the resulting integer is in
the range 0-2, choose white. If the integer equals 3, choose blue. If the integer is in the range 4-9, choose green.
Explanation / Answer
INCLUDE Irvine32.inc .data msgIntro byte "This is Your Name's fourth assembly extra credit program. Will randomly",0dh,0ah byte "choose between three different colors for displaying twenty lines of text,",0dh,0ah byte "each with a randomly chosen color. The color probabilities are as follows:",0dh,0ah byte "White=30%,Blue=10%,Green=60%.",0dh,0ah,0 msgOutput byte "Text printed with one of 3 randomly chosen colors",0 .code main PROC ;///////Intro Message///////////////////////////////// mov edx,OFFSET msgIntro ;intro message into edx call WriteString ;display msgIntro call Crlf ;endl call WaitMsg ;pause message call Clrscr ;clear screen call Randomize ;seed the random number generator mov edx, OFFSET msgOutput;line of text mov ecx, 20 ;counter (lines of text) L1:;//(Loop - Display Text 20 Times) call setRanColor ;calls random color procedure call SetTextColor ;calls the SetTextColor from library call WriteString ;display line of text call Crlf ;endl loop L1 exit main ENDP ;------------------------------------------------ setRanColor PROC ; ; Selects a color with the following probabilities: ; White = 30%, Blue = 10%, Green = 60%. ; Receives: nothing ; Returns: EAX = color chosen ;----------------------------------------------- mov eax, 10 ;range of random numbers (0-9) call RandomRange ;EAX = Random Number .IF eax >= 4 ;if number is 4-9 (60%) mov eax, green ;set text green .ELSEIF eax == 3 ;if number is 3 (10%) mov eax, blue ;set text blue .ELSE ;number is 0-2 (30%) mov eax, white ;set text white .ENDIF ;end statement ret setRanColor ENDP
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.