# 生产三个[0,1)的随机数相加为一

www.MyException.Cn  网友分享于：2013-02-20  浏览：20次

a=rnd(),b=rnd()

```# -*- coding:utf-8 -*-

from visual import *
import random

pointer_x = arrow(pos=(0,0,0), axis=(1,0,0), shaftwidth=0.01)
pointer_y = arrow(pos=(0,0,0), axis=(0,1,0), shaftwidth=0.01)
pointer_z = arrow(pos=(0,0,0), axis=(0,0,1), shaftwidth=0.01)

poslist=[];
colorlist=[color.red,color.yellow,color.green]

#方案一
for i in xrange(10000):
a=random.random()
b=random.random()
x1=min(a,b)
x2=abs(a-b)
x3=1-max(a,b)
points(pos=(x1,x2,x3), size=2, color=random.choice(colorlist))```

Double a=Math.random()

Double b=Math.random()*(1-a)

Double c=1-a-b

```# -*- coding:utf-8 -*-

from visual import *
import random

pointer_x = arrow(pos=(0,0,0), axis=(1,0,0), shaftwidth=0.01)
pointer_y = arrow(pos=(0,0,0), axis=(0,1,0), shaftwidth=0.01)
pointer_z = arrow(pos=(0,0,0), axis=(0,0,1), shaftwidth=0.01)

poslist=[];
colorlist=[color.red,color.yellow,color.green]

#方案二
for i in xrange(5000):
x1=random.random()
x2=random.uniform(0,1-x1)
x3=1-x1-x2
points(pos=(x1,x2,x3), size=2, color=random.choice(colorlist))```

生成图像：