184k views
5 votes
Write a program that randomly chooses among three different colors for displaying text on the screen. Use a loop to display 20 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%. Suggestion: Generate a random integer between 0 and 9. If the resulting integer falls in the range 0 to 2 (inclusive), choose white. If the integer equals to 3, choose blue. If the integer falls in the range 4 to 9 (inclusive), choose green. Test your program by running it ten times, each time observing whether the distribution of line colors appears to match the required probabilities.

1 Answer

3 votes

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

User IsidroGH
by
4.3k points