使用 rand(3) 生成 rand(9) [英] generate rand(9) using rand(3)

查看:71
本文介绍了使用 rand(3) 生成 rand(9)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你有一个函数 rand(3),它生成从 1 到 3 的随机整数.使用这个函数,构造另一个函数 rand(9),它生成从 1 到 9 的随机整数.

You have a function rand(3) which generates random integers from 1 to 3. Using this function, construct another function rand(9) which generates random integers from 1 to 9.

推荐答案

使用 rand(3) 两次,可以生成 3^2 对整数,即 (1,1), (1,2), ..., (3,3).将每一对分配给其中一个值 [1,9](例如 (1,1) 到 1、(1,2) 到 2 等)将为您提供 rand(9).象征性地:rand(9):=(rand(3),rand(3)).

Using rand(3) twice, one can generate 3^2 pairs of integers, i.e. (1,1), (1,2), ..., (3,3). Assigning each pair to one of the values [1,9] (e.g. (1,1) to 1, (1,2) to 2 etc.) will give you rand(9). Symbolically: rand(9):=(rand(3),rand(3)).

这篇关于使用 rand(3) 生成 rand(9)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆