j******n 发帖数: 2206 | 1 I use this formula
random_number=int(ranuni(0)*100000);
but it will give me 0, I don't want 0. if I add 1 to this formula,the
integer range will be 1 to 100001, which is alo not what I want.
I want exactly random integer numbers ranging from 1 to 100000.
Does anybody have an idea? |
D******n 发帖数: 2836 | 2 0<=ranuni(0)<=1
0<=int(ranuni(0)*100000)<=100000
1<=int(ranuni(0)*100000)+1<=100001
however the probability of getting 100001 = 0
usually u can sub the first ineq with 0 |
s******r 发帖数: 1524 | 3 int(ranuni(0)*99999)+1; ?
【在 j******n 的大作中提到】 : I use this formula : random_number=int(ranuni(0)*100000); : but it will give me 0, I don't want 0. if I add 1 to this formula,the : integer range will be 1 to 100001, which is alo not what I want. : I want exactly random integer numbers ranging from 1 to 100000. : Does anybody have an idea?
|
f***a 发帖数: 329 | 4 Rcode:
ceiling(runif(1)*100000) |
j******n 发帖数: 2206 | 5 thanks guys, they are all good ways.
真是晕了,这些方法都想不到。
谢拉 |
P****D 发帖数: 11146 | 6 嗯,习惯用这个。
【在 s******r 的大作中提到】 : int(ranuni(0)*99999)+1; ?
|
j******n 发帖数: 2206 | 7 来看看别人的讨论,very interesting,大家有兴趣的话,可以继续讨论一下。多谢了。
You could multiply by 99999 and add 1 as suggested in the Language
Reference.
You do realize that your variable will contain a particular integer
whenever ranuni returns a value in the range
[integer/100000,integer/100000). For example it will contain 52 for
any value from .00052 to .0005299999999999. However, it will contain
100000 only when ranuni returns 1.0 exactly. Your integer
distribution will not be uniform.
You would probably be better |
j******n 发帖数: 2206 | 8 another one:
PROC PLAN can produce a random permutation of the values 1-100000;
there will be no zero.
proc plan seed=1091883594;
factors r=100000 / noprint;
output out=r;
run;
proc print data=r(obs=100);
run;
proc print data=r(where=(r=0));
run; |
j******n 发帖数: 2206 | 9 when i run int(ranuni(0))*100000 的时候,我得到了起码两个zero,说明0和1还是
可能有的,所以
int(ranuni(0))*100000+1可能还是不太对。
【在 D******n 的大作中提到】 : 0<=ranuni(0)<=1 : 0<=int(ranuni(0)*100000)<=100000 : 1<=int(ranuni(0)*100000)+1<=100001 : however the probability of getting 100001 = 0 : usually u can sub the first ineq with 0
|
j******n 发帖数: 2206 | 10 ceil(0)还是0吧?
不知道rcode里面结果是什么
【在 f***a 的大作中提到】 : Rcode: : ceiling(runif(1)*100000)
|
|
|
D******n 发帖数: 2836 | 11 first, understand int ,floor and ceiling function
second, draw a diagram and see what happen. |
o****o 发帖数: 8077 | 12 %let n=100000;
%let seed=972675;
data _null_;
array _base{&n} p1-p&n;
retain seed &seed;
_prob=1/&n;
do _j=1 to dim(_base); _base[_j]=_prob; end;
chksum=sum(of p1-p&n);
call rantbl(seed, of p1-p&n, rv);
put chksum= rv=;
run;
not so efficient comparing to PROC PLAN but should be very handy when you
need one rv on the fly
了。
【在 j******n 的大作中提到】 : 来看看别人的讨论,very interesting,大家有兴趣的话,可以继续讨论一下。多谢了。 : You could multiply by 99999 and add 1 as suggested in the Language : Reference. : You do realize that your variable will contain a particular integer : whenever ranuni returns a value in the range : [integer/100000,integer/100000). For example it will contain 52 for : any value from .00052 to .0005299999999999. However, it will contain : 100000 only when ranuni returns 1.0 exactly. Your integer : distribution will not be uniform. : You would probably be better
|
p********a 发帖数: 5352 | 13 ranuni(0) could be 0 or 1? I thought it is something between 0 and 1 but not
equal to 0 or 1 |
D******n 发帖数: 2836 | |
D******n 发帖数: 2836 | 15 I think lz is confused by two different situations, the first is 0
the second one is 0<= int(rand) |
p********a 发帖数: 5352 | 16 Then what is the best way to get random numbers between 1 and N? |
f***a 发帖数: 329 | 17 From the description of "runif()"
"runif will not generate either of the extreme values unless max = min or
max-min is small compared to min, and in particular not for the default
arguments."
It seems 0 won't be generated, so ceiling(runif(1)*N) should works OK.
【在 j******n 的大作中提到】 : ceil(0)还是0吧? : 不知道rcode里面结果是什么
|