#include <memory>
int random_int(int min, int max) {
return min + *(int*)((char*)alloca(1) - 42) % (max - min);
}
Well, it mostly gives you a random value, depending on what's on the stack at any given moment.
There are a "few" issues with this:
Using alloca. It's considered a bad practice in most cases, though it's useful sometimes.
There are no guarantees that the accessed memory space won't crash the program. Though in most cases you'll be at least 42 bytes deep in the stack.
There is no guarantee that you have 1 more byte available on the stack.
No checks to make sure max > min.
min and max is already present used if you include <windows.h> in the source file including this header, so you need to use NOMINMAX (or something like that) before including windows.h.
#include <cstdlib>
#include <iostream>
using namespace std;
unsigned long long rand(unsigned long long min, unsigned long long max) {
static const unsigned long long N = 78932;
static unsigned long long seed = 2981987505890893489UL;
unsigned long long *n =
(unsigned long long *)malloc(sizeof(unsigned long long) * N);
unsigned long long ulln = (unsigned long long)n;
unsigned long long r = ulln & (seed += ulln);
for (unsigned long long i = 1; i < N; i++) {
if (i & 1) {
if (n[i] != 0) {
r = r % n[i];
}
} else {
r = r * n[i];
}
}
r = (r % (max - min)) + min;
return r;
}
int main() {
for (int i = 0; i < 10; i++) {
cout << rand(0, 99) << endl;
}
return 0;
}
This is so bad I can't guarantee that it's random. It calls malloc to get random data and then transforms that data based on a seed and the data from malloc. It doesn't call free so we can never allocate the same memory and therefore makes it harder to duplicate results therefore more randomness. Also includes "using namespace std;"
Core Algorithm: check how many digits are in the upper bound. Get the current time in milliseconds. Get the last digits in the current time to match the decimal length of the upper bound. Check if this results is between the bounds. If so, done. If not, loop.
Lots of bad practices in this code. I typed it on my phone with an online IDE for extra bad formatting (whitespace, curly braces, etc). No error or input checking. Unnecessary "while(true)" just to feel like it could go forever. Bad method and variable names.
It seems to work for the given test case. Outputs two different numbers (most of the time). In badcode fashion, I did zero testing outside the provided test case.
Sorry that I had to upload it to Github, but the file was too large to just put in a comment here.
Example of the code just to showcase:
module main;
import std.stdio : writeln;
import std.datetime : Clock;
int getRandomNumber()
{
int c = 0;
auto time = Clock.currTime();
int result = int.max;
int h = time.hour;
int m = time.minute;
int s = time.second;
int d = time.dayOfYear;
if (h == 0 && m == 0 && s == 0) result = 0;
if (h == 0 && m == 0 && s == 1) result = 1;
if (h == 0 && m == 0 && s == 2) result = 2;
if (h == 0 && m == 0 && s == 3) result = 3;
...
if (h == 23 && m == 59 && s == 52) result = 86392;
if (h == 23 && m == 59 && s == 53) result = 86393;
if (h == 23 && m == 59 && s == 54) result = 86394;
if (h == 23 && m == 59 && s == 55) result = 86395;
if (h == 23 && m == 59 && s == 56) result = 86396;
if (h == 23 && m == 59 && s == 57) result = 86397;
if (h == 23 && m == 59 && s == 58) result = 86398;
if (h == 23 && m == 59 && s == 59) result = 86399;
return (result * d) / c;
}
void main()
{
writeln(getRandomNumber());
}
Each time randomInt is called, it reads off a sufficient number of digits from the extremely long string of numbers (using the IEnumerable/yield return pattern). If that set of digits is less than or equal to the range, it's kept. Otherwise, a new set of digits is collected. Once each digit from the string has been read once, the pointer resets and it starts from the beginning of the string again.
Calling it multiple times with the same arguments will produce different results because of the static randomDigitGenerator that persists through calls. However, each time you run the program it will start from place, so it will produce the same random number each program execution.
The algorithm is actually quite solid, but it's not secure by any means and basing your random numbers on a several thousand character long string isn't exactly ideal.
I wrote a solution that uses the Python "turtle" module. In order to generate a random number, the user has to draw a picture with the turtle. Here's a demo video: https://www.youtube.com/watch?v=wIJVAE2Hu3I
import turtle
drawing_sum = 0
def move_bob(x, y):
global drawing_sum
bob.ondrag(None)
bob.setheading(bob.towards(x, y))
bob.goto(x, y)
x = int(x)
y = int(y)
color = (x % 255, y % 255, (x + y) % 255)
bob.color(color)
drawing_sum += (x + y) * sum(color)
bob.ondrag(move_bob)
def random_int(min, max):
bob.write('Please draw a random picture', align='center', font=('Calibri', 20, 'normal'))
bob.penup()
bob.setpos(0, -50)
bob.pendown()
bob.pensize(4)
bob.turtlesize(3, 3, 3)
bob.shape('turtle')
bob.speed('fastest')
bob.getscreen().colormode(255)
bob.ondrag(move_bob)
bob_screen = bob.getscreen()
bob_screen.mainloop()
return drawing_sum % (max - min) + min
bob = turtle.Pen()
print(random_int(0, 99))
14
u/sirpalee Mar 12 '19
Well, it mostly gives you a random value, depending on what's on the stack at any given moment.
There are a "few" issues with this:
8
u/jamolnng Mar 12 '19
This is so bad I can't guarantee that it's random. It calls malloc to get random data and then transforms that data based on a seed and the data from malloc. It doesn't call free so we can never allocate the same memory and therefore makes it harder to duplicate results therefore more randomness. Also includes "using namespace std;"
4
u/SinisterMinister42 Mar 12 '19
Here's my submission.
Core Algorithm: check how many digits are in the upper bound. Get the current time in milliseconds. Get the last digits in the current time to match the decimal length of the upper bound. Check if this results is between the bounds. If so, done. If not, loop.
Lots of bad practices in this code. I typed it on my phone with an online IDE for extra bad formatting (whitespace, curly braces, etc). No error or input checking. Unnecessary "while(true)" just to feel like it could go forever. Bad method and variable names.
It seems to work for the given test case. Outputs two different numbers (most of the time). In badcode fashion, I did zero testing outside the provided test case.
3
u/bausscode Mar 17 '19
Since there are no restrictions on whether there should be min,max or that the number has to be in a specific range here is my result: https://raw.githubusercontent.com/bausshf/The-Worst-Random-Generator/master/source/main.d
Sorry that I had to upload it to Github, but the file was too large to just put in a comment here.
Example of the code just to showcase:
3
u/[deleted] Mar 17 '19
https://pastebin.com/RiEeQ9Dj
oh god...
2
u/TheUnlocked Mar 12 '19 edited Mar 12 '19
C# (see replies for algorithm explanation)
2
u/[deleted] Mar 13 '19 edited Mar 13 '19
I wrote a solution that uses the Python "turtle" module. In order to generate a random number, the user has to draw a picture with the turtle. Here's a demo video: https://www.youtube.com/watch?v=wIJVAE2Hu3I
0
u/[deleted] Mar 12 '19
[deleted] — view deleted comment