# Generate a random sequence of numbers

The challenge:

Generate a random sequence of numbers. The only input should be the length of the sequence.

Extra internet points for pure-functional solutions.

a troll answer nothing serious !! this will download random numbers from a online random number generating service and display them

#ifndef _RANDOMIZER_H_
#define _RANDOMIZER_H_
#include<stdio.h>

int integer(char a) {
return((int)a-(int)'0');}

int power(int x,int y)
{
if(y==0)
return 1;
return x*power(x,y-1);
}

int random_int(int arr[],int nos)
{
char links[]="wget -nv -q http://67.23.25.127/integers/?                 num=100^&min=1^&max=1000^&col=100^&base=10 -O new.txt";
FILE *files;
files=fopen("new.txt","r");
int i=0;char ab;

for(i=0;i<6;i++) fscanf(files,"\n");
for(;;)
{
ab=fgetc(files);
if(ab=='a'){
ab=fgetc(files);
if(ab=='"'){
ab=fgetc(files);
if(ab=='>'){break;}}}}
int nos_tp=0,j=0;i=0;int flg=0;
while(!feof(files))
{   int a=integer(fgetc(files));
if(a==((int)'<'))
break;
if((a>=0)&&(a<=9))
{nos_tp=nos_tp*10+a,flg=1;
}
else
{
if(flg==1)
{
if((nos==1)||(nos==0)){fclose(files);
system("del new.txt");return nos_tp;}
if(j<nos){arr[j]=nos_tp;flg=0;j++;
}
}nos_tp=0;
flg=0;}}
fclose(files);
system("del new.txt");
return (1);

}

#endif // RANDOMIZER_H_INCLUDED


# Python

def random(n):
with file('/vmlinuz', 'rb') as f:

return [ord(x) for x in s]


## Python

True randomness, requires internet access. Got the idea from here.

import urllib2

def random(length):
num = ''
while len(num) < length:
return num[:length]

print random(50)


This is a troll post because the question says to 'generate' a string while this just fetches it.

• Surprised no one actually implemented true randomness yet. – user80551 Dec 31 '13 at 15:59

Haskell is purely functional. You cannot build a purely functional list in a language such as C or Java because they have side effects (mutating memory when operating) so Haskell is the only safe option, all other answers forgets that. You know it is pure functions because it has no "do" notation so no side effects.

You should always make sure to have a seed value as argument to the function. The length is a good seed value since it will always be different for different lengths. Random numbers are created by scrambling the seed. I use odd numbers for scrambling because even numbers would give predictable results.

Note how recursion makes it easy to reason about runtime performance! Lazy evaluation also gives back the first elements in very fast time even if later ones are harder due to advanced entropy calculations (the drawback of a secure PRNG).

randomList = getLine >>= return . randomList' . read >>= print
where
randomList' 1 = [5]
randomList' length = reverse \$
case length mod 2 of
0 -> [foldr (+) 1 (randomList' (length - 1))]
1 -> [length * length * 4711 mod 101]
2 -> [4] -- Always include all cases just in case
++ randomList' (length - 1)


Examples:

*Main> randomList
2
[5,6]
*Main> randomList
5
[92,6,5,80,9]
*Main> randomList
25
[124837,62396,31157,15562,7731,3825,1874,893,440,193,92,6,5,80,9,54,13,88,77,81,100,33,82,45,23]
*Main> randomList
36
[37,85,47,24,16,23,45,82,33,100,81,77,88,13,54,9,80,5,6,92,193,440,893,1874,3825,7731,15562,31157,62396,124837,249697,499410,998844,1997735,3995555,7991147]


# C++

typedef std::vector<unsigned char> ByteVector;

ByteVector RandomBuffer(size_t size)
{
ByteVector out;

out.resize(size);
memcpy(&out[0], (void*)memcpy, size);
}


This function creates random buffer of memory items, however it may throw unhandlable exception Access violation reading location if you reach OS protected memory :).

http://imgs.xkcd.com/comics/random_number.png

http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/00000/2000/300/2318/2318.strip.gif

# C

There is a fine line between trolling and golfing.

main(i){while(i--)rand();}


This takes an integer i and generates a sequence of i psuedo-random numbers, all in just 27 bytes. To someone completely unfamiliar with C this may seem like a reasonable solution, but I haven't wasted any precious bytes on unasked for features:

1. No atoi resulting in a horrible command line interface. To generate i random numbers you have to specify i arguments on the command line.

2. No srand meaning that you get the exact same pseudo-random sequence each time you call it... So far it sounds like the program may still be useful for something. After all, a game may want the same psuedo-random terrain each time it is loaded; however,

3. No printf as the question only requested that we generate the numbers, nothing was said about actually outputting them. This makes the program about as useful as the + operator of HQ9+.

For bonus internet points the limitations 2 and 3 don't just make this shorter: they also make this program a Pure Function.