XP is just a number PerlMonks

Re^4: generate random number without using any built in function in perl?

by BrowserUk (Pope)
 on May 09, 2013 at 10:55 UTC ( #1032759=note: print w/replies, xml ) Need Help??

```C:\test>rand -M=7 -N=1e5
{ "0" => 13948, 1 => 14522, 2 => 14752, 3 => 14208, 4 => 14032, 5 => 1
+4408, 6 => 14130 }
...
C:\test>rand -M=100 -N=1e5
{ "0" => 1029, 1 => 1110, 2 => 977, 3 => 1031, 4 => 1008, 5 => 1137, 6
+ => 998, 7 => 1169, 8 => 915, 9 => 1039, 10 => 879, 11 => 1149, 12 =>
+ 967, 13 => 972, 14 => 954, 15 => 967, 16 => 1219, 17 => 1009, 18 =>
+870, 19 => 858, 20 => 1005, 21 => 1077, 22 => 914, 23 => 930, 24 => 9
+87, 25 => 951, 26 => 979, 27 => 1029, 28 => 1056, 29 => 902, 30 => 96
+5, 31 => 810, 32 => 979, 33 => 1101, 34 => 1009, 35 => 1190, 36 => 86
+8, 37 => 1013, 38 => 981, 39 => 1104, 40 => 905, 41 => 993, 42 => 937
+, 43 => 1018, 44 => 1098, 45 => 1053, 46 => 898, 47 => 908, 48 => 994
+, 49 => 1135, 50 => 1174, 51 => 1010, 52 => 967, 53 => 1025, 54 => 99
+5, 55 => 1029, 56 => 875, 57 => 1129, 58 => 908, 59 => 976, 60 => 906
+, 61 => 922, 62 => 962, 63 => 931, 64 => 1044, 65 => 999, 66 => 980,
+67 => 838, 68 => 973, 69 => 1140, 70 => 949, 71 => 965, 72 => 1044, 7
+3 => 940, 74 => 927, 75 => 1107, 76 => 925, 77 => 1045, 78 => 968, 79
+ => 996, 80 => 890, 81 => 1123, 82 => 119
8 => 990, 89 => 966, 90 => 1072, 91 => 927, 92 => 915, 93 => 1093, 94
+=> 1063, 95 => 966, 96 => 1056, 97 => 994, 98 => 844, 99 => 1086 }
```
```...
my \$bytes = join '', map chr( unpack( 'V', \$rand ) % 256 ), 1 .. \$N;
...

C:\test>rand -N=1e6
In 1e6 bytes, there are 3997975 ones and 4002025 zeros
```
```C:\test>rand -N=1e6
...
...
In 1e6 bytes, there are 3991425 ones and 4008575 zeros
Stddev: 197.76
{ 2 => 500058, 3 => 248757, 4 => 124893, 5 => 62380, 6 => 30268, 7 =>
+24800, 8 => 4693, 9 => 582, 10 => 153, 11 => 59, 12 => 26, 13 => 13,
+14 => 10, 15 => 11, 16 => 108, 17 => 45, 18 => 14, 19 => 11, 20 => 5,
+ 21 => 8, 22 => 7, 23 => 2, 24 => 46, 25 => 13, 26 => 10, 27 => 6, 28
+ => 5, 29 => 3, 30 => 3, 31 => 1, 32 => 50, 33 => 10, 34 => 6, 35 =>
+3, 36 => 5, 37 => 3, 38 => 1, 39 => 1, 40 => 40 }
```

Create A New User
Node Status?
node history
Node Type: note [id://1032759]
help
Chatterbox?
 [Eily]: you could tie a variable into not having the same value each time, if you like to make people who try to debug your code facepalm [Corion]: perl -wle 'package o; use overload q("") => sub {warn "str"; ""}, bool => sub{warn "bool"; 1}; package main; my \$o={}; bless \$o => o; print "Yay" if (\$o && !length(\$o))' [Corion]: But people writing such code should document the objects they construct and why it makes sense for an object to be invisible as string while being true in a boolean context [hippo]: That's equal parts clever and horrendous. [Eily]: the overload version wouldn't return true with "\$x" && !length \$x though, I guess [hippo]: The more I look at this code, the more \$x is a plain old scalar and the more this condition will never be true. I'm calling it a bug at this point. [hippo]: Thanks for your input which has soothed my sanity (a little) [Corion]: Eily: Sure - if you force both things into stringy things, then you break that magic. But that would also mean that you changed the expression, as now \$x = 0.00 will be true instead of false as it were before [Corion]: Ah no, at least in my feeble experiments that doesn't change the meaning [Corion]: We sell sanity in small packages ;)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2017-07-27 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (413 votes). Check out past polls.