简体   繁体   English

确定最高随机数的“模式”

[英]Determine “Mode” for Highest Randomized Number

I am working on a Turing application that displays a random number, and then displays how many times that number is rolled, but then finds the mode, the highest value of the amount of times the random is rolled, and displays which number (1-6) is rolled the most. 我正在使用显示随机数的图灵应用程序,然后显示该数字的滚动次数,然后查找模式,随机滚动次数的最高值,并显示该数字(1- 6)滚动最多。 Thanks for the help. 谢谢您的帮助。

var numberDice : int
var freq : array 1 .. 6 of int
var highestNum : int

highestNum := 0

for j : 1 .. 6
    freq (j) := 0
end for


for i : 1 .. 25
    randint (numberDice, 1, 6)

    freq (numberDice) := freq (numberDice) + 1


end for

for counter : 1 .. 6
    if freq (counter) > highestNum then
        highestNum := freq (counter)
    end if
end for


put "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

put "Number             Frequency"
put "1                  ", freq (1)
put "2                  ", freq (2)
put "3                  ", freq (3)
put "4                  ", freq (4)
put "5                  ", freq (5)
put "6                  ", freq (6)

put " "
put "Mode: ", highestNum
put "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

It's great that people are still using Turing. 人们还在使用图灵,真是太棒了。 It is the best beginner's language hands down. 这是最好的初学者的语言。

Your current code captures the maximum frequency value . 您当前的代码捕获最大频率 You also want to capture the index of the maximum frequency value . 您还想捕获最大频率值索引 Look carefully at your code to decide where the index value you need can be or is being computed. 仔细查看您的代码,以确定您需要或正在计算所需索引值的位置。 Declare a variable to capture it, say highestIndex . 声明一个变量来捕获它,比如highestIndex Then add code to do the capturing and printing. 然后添加代码进行捕获和打印。 I think it will take 3 lines. 我认为需要3行。 What are they? 这些是什么? Where do they go? 他们去哪里? When you understand how the current program works, you will see... 当你了解当前程序的工作原理时,你会看到......

You need an additional variable to hold the die you found to be highest: 你需要一个额外的变量来保存你发现最高的骰子:

var highestNum : int
var highestIndex : int

highestNum := 0
highestIndex := 0

. . .

    highestNum := freq (counter)
    highestIndex := counter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM