12.19随机数生成

具体代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <title>Math.random方法彩票随机数的生成</title>

</head>

<body>

 <!-- 设置样式 -->

 <input type="text" id="text">

 <button id="btnGo">开始</button>

 <button id="btnStop">获取随机数组</button>

 

 <script type="text/javascript">

 

 //获取节点

 var btnGo = document.getElementById("btnGo");

 var btnStop = document.getElementById("btnStop");

 var text = document.getElementById("text");

 //定义生成最小到最大值的随机函数

 function rand(min,max){

  return parseInt(Math.random()*( max - min + 1) + min);

 }

  

 function start(min,max,length){

  //定义空数组

  var arr = [];

 

  while(arr.length<length){

   //生成一个随机数prem

  var prem=rand(min,max);

  //判断生成的随机数prem是否在数组arr里,果然不在,就将这个随机数插入到数组里,如果在,执行下一次循环

  if(arr.indexOf(prem) == -1){

 

   arr.push(prem);

  }

  }

  //返回数组arr

  return arr;

 }

 

 var timer = 0;

 //单击开始按钮生成随机数组

 btnGo.onclick =function(){

  //清除

  clearInterval(timer);

  timer = setInterval(function() {

  text.value = start(1,33,7);

 },50)

 }

 //单击停止按钮获取一组随机数

 btnStop.onclick =function(){

  clearInterval(timer);

 }

  

 </script>

</body>

</html>

代码如下:""" File: fromexample.py Project 12.9 Defines and tests the all pairs shortest paths algorithm of Floyd. Uses the graph from Figure 12.19 of the text, as represented in the file example.txt. """ from graph import LinkedDirectedGraph import random from arrays import Array # Functions for working with infinity def isLessWithInfinity(a, b): """Returns False if a == b or a == INFINITY and b != INFINITY. Otherwise, returns True if b == INFINITY or returns a < b.""" if a == LinkedDirectedGraph.INFINITY and b == LinkedDirectedGraph.INFINITY: return False elif b == LinkedDirectedGraph.INFINITY: return True elif a == LinkedDirectedGraph.INFINITY: return False else: return a < b def addWithInfinity(a, b): """If a == INFINITY or b == INFINITY, returns INFINITY. Otherwise, returns a + b.""" if a == LinkedDirectedGraph.INFINITY or b == LinkedDirectedGraph.INFINITY: return LinkedDirectedGraph.INFINITY else: return a + b def minDistance(a, b): if isLessWithInfinity(a, b): return a else: return b # Define a function that uses Floyd's algorithm def allPairsShortestPaths(matrix): """ please complete the Floyd algorithm here """ pass # Define a function to print a labeled distance matrix def printDistanceMatrix(matrix, table): """Prints the distance matrix with rows and columns labels with the index positions and vertex labels.""" labels = Array(len(table)) index = 0 labelWidth = 0 indexWidth = 0 for label in table: labels[table[label]] = label labelWidth = max(labelWidth, len(str(label))) indexWidth = max(indexWidth, len(str(index))) index += 1 weightWidth = 0 for row in range(matrix.getHeight()): for column in range(matrix.getWidth()): weightWidth = max(weightWidth, len(str(matrix[row][column]))) weightWidth = max(weightWidth, labelWidth, indexWidth) topRowLeftMargin
最新发布
04-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值