Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. int rand7(){int s=0,c=7;while(c-->0)s+=rand5();return s%7+1;}
  2.  
  3. class Rand {
  4.  
  5. public static void main(String[] args) {
  6. int[] nums = new int[7];
  7. // get a lot of numbers
  8. for(int i = 0; i < 10000000; i++) nums[rand7()-1]++;
  9. // print the results
  10. for(int i = 0; i < 7; i++) System.out.println((i+1) + ": " + nums[i]);
  11. }
  12.  
  13. // just for rand5()
  14. static java.util.Random r = new java.util.Random();
  15.  
  16. static int rand5() {
  17. return r.nextInt(5)+1; // Random.nextInt(n) returns 0..n-1, so add 1
  18. }
  19.  
  20. static int rand7(){int s=0,c=7;while(c-->0)s+=rand5();return s%7+1;}
  21.  
  22. }
  23.  
  24. C:Documents and SettingsglowcoderMy Documents>java Rand
  25. 1: 1429828
  26. 2: 1429347
  27. 3: 1428328
  28. 4: 1426486
  29. 5: 1426784
  30. 6: 1429853
  31. 7: 1429374
  32.  
  33. C:Documents and SettingsglowcoderMy Documents>
  34.  
  35. function Rand7(){for(i=r=0;i<7;i++)r+=Rand5()%2;return r}
  36.  
  37. function Rand7(){for(i=r=0;i<7;i++)r+=Rand5()%2;return r}
  38.  
  39. sub rand7{($x=5*&rand5+&rand5-3)<24?int($x/3):&rand7}
  40.  
  41. sub rand7{($x=5*&rand5+&rand5)<30?$x%7+1:&rand7}
  42.  
  43. def rand7;x=8;while x>7 do x=rand5+5*rand5-5 end;x;end
  44.  
  45. def rand7;x=rand5+5*rand5-5;x>7 ?rand7: x;end
  46.  
  47. def Rand7():
  48. while True:
  49. x = (Rand5() - 1) * 5 + (Rand5() - 1)
  50. if x < 21: return x/3 + 1
  51.  
  52. (defun rand7()(let((n(-(+(rand5)(* 5(rand5)))5)))(if(> n 7)(rand7)n)))
  53.  
  54. int rand7(){int x=8;while(x>7)x=rand5()+5*rand5()-5;return x;}
  55.  
  56. function Rand7(){$x=8;while($x>7)$x=rand5()+5*rand5()-5;return $x;}
  57.  
  58. def rand7:Int={val r=5*(rand5-1)+rand5
  59. if(r<8)r else rand7}
  60.  
  61. 1 2 3 4 5
  62. 1 1 2 3 4 5
  63. 2 6 7 8 ..
  64. 3 11 ..
  65. 4 ..
  66. 5
  67.  
  68. def rand7=(1 to 7).map(_=>rand5).sum%7+1
  69.  
  70. def rand7=((0/:(1 to 7))((a,_)=>a+rand5-1))%7+1
  71.  
  72. val rnd = util.Random
  73. def rand5 = rnd.nextInt (5) + 1
  74.  
  75. int rand7(){int x=rand5()*5+rand5()-6;return x>21?rand7():x/3+1;}
  76.  
  77. int rand7(){int x=rand5()+5*rand5()-5;return x>7?rand7():x;}
  78.  
  79. function Rand7(){x=8;while(x>7)x=rand5()+5*rand5()-5;return x}
  80.  
  81. rand7 = lambda: sum(rand5() for i in range(7)) % 7 + 1
  82.  
  83. counter = [0] * 7
  84. for i in range(100000):
  85. counter[rand7()] += 1
  86.  
  87. rand7=lambda:eval("+rand5()"*7)%7+1
  88.  
  89. unsigned int Rand4()
  90. {
  91. unsigned int r = Rand5();
  92.  
  93. while(r == 5)
  94. r = Rand5();
  95.  
  96. return(r);
  97. }
  98.  
  99. unsigned int myRand2()
  100. {
  101. return((Rand4() - 1) & 1);
  102. }
  103.  
  104. unsigned int Rand7()
  105. {
  106. return(((myRand2() << 2) | (myRand2() << 1) | myRand2()) + 1);
  107. }
  108.  
  109. int Rand7(){int n=0;for(int i=0;i<3;++i){int r=Rand5();while(r==5) r=Rand5();--r;r&=1;n|=r<<i;} return n;}
  110.  
  111. # Construct a Rand5 function
  112. Rand5 <- function() sample(seq(5),1)
  113. # And the golf
  114. Rand7=function(r=Rand5())sample(1:(r/r+6),1)
  115. # Or (same character count)
  116. Rand7=function(r=Rand5())sample.int(r/r+6,1)
  117. # Or even shorter(thanks to @Spacedman)
  118. Rand7=function()sample(7)[Rand5()]
  119.  
  120. > test <- replicate(10^6,Rand7())
  121. > table(test)
  122. test
  123. 1 2 3 4 5 6 7
  124. 142987 142547 143133 142719 142897 142869 142848
  125.  
  126. library(ggplot2)
  127. qplot(test)
  128.  
  129. (defn rand7[](#(if(<% 8)%(rand7))(+(rand5)(*(rand5)5)-5)))
  130.  
  131. function Rand7(){for(x=0,i=1;i<8;x^=i*((k=Rand5())%2),i*=1+(k<5));return x?x:Rand7()}
  132.  
  133. int Rand7()
  134. {
  135. int r = Rand5();
  136. int n = 5;
  137. do {
  138. r = (r - 1) * 5 + Rand5();
  139. int m = n * 5 / 7 * 7;
  140. if (r <= m) {
  141. return r % 7 + 1;
  142. }
  143. r -= m;
  144. n = n * 5 - m;
  145. } while (1);
  146. }
  147.  
  148. 142935 142751 142652 143299 142969 142691 142703
  149.  
  150. 1 2 3 4 5 6 7 8 9 10
  151. 0 840180 112222 44433 2212 886 0 60 6 1
  152.  
  153. def rand7():
  154. while True:
  155. n=5*(rand5()-1)+(rand5()-1)
  156. if n<21:return n%7+1
  157.  
  158. int rand7(){int r;do{r=rand5()+5*rand5()-5;}while(r>7);return r;}
  159.  
  160. int result = 0;
  161.  
  162. for (int i = 0; i++; i<7)
  163. if (((rand(5) + rand(5)) % 2) //check if odd
  164. result += 1;
  165.  
  166. return result + 1;
  167.  
  168. rand7=function(n)sample(7,n,T)
  169.  
  170. > rand7(20)
  171. [1] 4 3 6 1 2 4 3 2 3 2 5 1 4 6 4 2 4 6 6 1
  172. > rand7(20)
  173. [1] 1 2 5 2 6 4 6 1 7 1 1 3 7 6 4 7 4 2 1 2
  174. > rand7(20)
  175. [1] 6 7 1 3 3 1 5 4 3 4 2 1 5 4 4 4 7 7 1 5
  176.  
  177. sub rand7{1while($_=5*&rand5-rand5)>6;$_+1}
  178.  
  179. sub rand7{1while($_=5*&rand5-rand5)>20;$_%7+1}
  180.  
  181. rand7={if(b==null)b=rand5();(b=(rand5()+b)%7+1)}
  182.  
  183. [1:5030, 2:4909, 3:5017, 4:4942, 5:5118, 6:4956, 7:5028]
  184.  
  185. Rand7=Rand5[]~Sum~{7}~Mod~7+1&
  186.  
  187. 00000000 2f 72 61 6e 64 37 7b 38 7b 92 38 37 92 61 7b 92 |/rand7{8{.87.a{.|
  188. 00000010 40 7d 69 66 92 75 32 7b 72 61 6e 64 35 7d 92 83 |@}if.u2{rand5}..|
  189. 00000020 35 92 6c 92 01 35 92 a9 7d 92 65 7d 92 33 |5.l..5..}.e}.3|
  190. 0000002e
  191.  
  192. % This is the actual rand7 procedure.
  193. /rand7{
  194. 8{ % potentialResult
  195. % only if the random number is less than or equal to 7, we're done
  196. dup 7 le{ % result
  197. exit % result
  198. }if % potentialResult
  199. pop % -/-
  200. 2{rand5}repeat % randomNumber1 randomNumber2
  201. 5 mul add 5 sub % randomNumber1 + 5*randomNumber2 - 5 = potentialResult
  202. }loop
  203. }def
  204.  
  205. %Now, some testing code.
  206.  
  207. % For testing, we use the built-in rand operator;
  208. % Doesn't really give a 100% even distribution as it returns numbers
  209. % from 0 to 2^31-1, which is of course not divisible by 5.
  210. /rand5 {
  211. rand 5 mod 1 add
  212. }def
  213.  
  214. % For testing, we initialize a dict that counts the number of times any number
  215. % has been returned. Of course, we start the count at 0 for every number.
  216. <<1 1 7{0}for>>begin
  217.  
  218. % Now we're calling the function quite a number of times
  219. % and increment the counters accordingly.
  220. 1000000 {
  221. rand7 dup load 1 add def
  222. }repeat
  223.  
  224. % Print the results
  225. currentdict{
  226. 2 array astore ==
  227. }forall
  228.  
  229. int Rand7()
  230. {
  231. return Rand5()+ Rand5()/2;
  232. }
  233.  
  234. static unsigned int gi = 0;
  235.  
  236. int rand7()
  237. {
  238. return (((rand() % 5 + 1) + (gi++ % 7)) % 7) + 1;
  239. }
  240.  
  241. //call this seed before rand7
  242. //maybe it's not best seed, if yo have any good idea tell me please
  243. //and thanks JiminP again, he remind me to do this
  244. void srand7()
  245. {
  246. int i, n = time(0);
  247. for (i = 0; i < n % 7; i++)
  248. rand7();
  249. }
  250.  
  251. 1st get one number from 1 2 3 4 5 by rand5
  252. 2nd get one number from 2 3 4 5 6
  253. 3rd get one number from 3 4 5 6 7
  254. 4th get one number from 4 5 6 7 1
  255. 5th get one number from 5 6 7 1 2
  256. 5th get one number from 6 7 1 2 3
  257. 7th get one number from 7 1 2 3 4
  258.  
  259. 8th get one number from 1 2 3 4 5
  260. 9th get one number from 2 3 4 5 6
  261. ......
  262.  
  263. (((rand() % 5 + 1) + (gi++ % 7)) % 7) + 1
  264.  
  265. #include <stdio.h>
  266. #include <stdlib.h>
  267. #include <time.h>
  268.  
  269. static unsigned int gi = 0;
  270.  
  271. //a = rand() % 5 + 1 is rand5 in C language,
  272. //b = gi++ % 7 generates all permutations,
  273. //c = (a + b) % 7 + 1, generates 1 - 7 uniformly.
  274. //Dont forget call srand7 before rand7
  275. int rand7()
  276. {
  277. return (((rand() % 5 + 1) + (gi++ % 7)) % 7) + 1;
  278. }
  279.  
  280. //call this seed before rand7
  281. //maybe it's not best seed, if yo have any good idea tell me please
  282. //and thanks JiminP again, he remind me to do this
  283. void srand7()
  284. {
  285. int i, n = time(0);
  286. for (i = 0; i < n % 7; i++)
  287. rand7();
  288. }
  289.  
  290. void main(void)
  291. {
  292. unsigned int result[10] = {0};
  293. int k;
  294.  
  295. srand((unsigned int)time(0)); //initialize the seed for rand
  296. srand7() //initialize the rand7
  297.  
  298. for (k = 0; k < 100000; k++)
  299. result[rand7() - 1]++;
  300.  
  301. for (k = 0; k < 7; k++)
  302. printf("%d : %.05fn", k + 1, (float)result[k]/100000);
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement