random function

Just starting out? Need help? Post your questions and find answers here.
Pantcho!!
Enthusiast
Enthusiast
Posts: 538
Joined: Tue Feb 24, 2004 3:43 am
Location: Israel
Contact:

random function

Post by Pantcho!! »

hello,

sorry for the super noob question but,

how can i write my own random function??

thinking about it that a computer can pick a random number sounds
so unlogical to me!

i wish to also know how it is being done.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Most random functions are based on seed numbers and sometimes the time function get used, then some maths is performed on the seed, usually some kind of multiplication and an add and then the new number is used to make the new seed number.

Sounds complicated but it isn't really. The trick is trying to get the numbers to appear random.

Most computers return the same set of numbers each time if you set the seed to the same number.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

yo hi.

i had some code on my harddisk (sent to me by a mate, not my code, but free-to-share)

Code: Select all

#m=100000000 
#m1=10000 
#b=31415821 

i.l 
Global a.l 
N.l 

Procedure mult(p.l,q.l) 
  p1.l 
  p0.l 
  q1.l 
  q0.l 
  p1=Int(p/#m1) 
  p0=p%#m1 
  q1=Int(q/#m1) 
  q0=q%#m1 
  ProcedureReturn (((p0*q1+p1*q0)%#m1)*#m1+p0*q0)%#m 
EndProcedure 

Procedure myrandom(r)  ;r=max 
  a=(mult(a,#b)+1)%#m 
  ProcedureReturn Int((Int(a/#m1)*r)/#m1)+1  ;numbers 1 to r 
EndProcedure 

N=10 
a=25;Random(12135)   ;seed 
For i=1 To N 
  Debug myrandom(11)  
Next
a random function is ANY function, that produces a row of numbers,
wich spreading is almost regular, meaning, none is significantly more often than another.

you could also use a sinus with a very short phase, to produce random numbers,
but with a sinus you will have significant more numbers at the border than in the middle...

PS:
historic remark:
the only true random ever was possible on the C64.
you could read the low-byte of the oscillator of the sound chip.
when you set the waveform to white noise, the values were perfectly random.

all other random functions are just seeming to be random, but are completely predictive.

(like Derek already stated, have read his post only after submitting mine)
oh... and have a nice day.
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

Well there is near to true randomness under Linux aswell, there is the /dev/random dive that uses system noise to create random numbers and then there is /dev/urandom which usses the truely random seeds from the system to produce random numbers even when the /dev/random device is out of data. On some Intel MoBos these two functions may also use Hardware based Random number generators. If you want even more randomnes there are Hardware Random Number generators which for example use radioactive decay to generate absolutely unpredictable data. Because system noise can sometimes be tricked by putting the system in a vey loud environment playing montoneous sounds.
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

@KG: That was my code, translated from a Pascal code by Robert
Sedgewick, if I remember correctly :wink:
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

@remi
well, Leo gave it to me, i dunno, where it was from.
so, I hope it's ok that I posted it here.
I got it from Leo because I couldn't find some old C code i had in one of my books....
I was asked to create some Random-Class for the OOP-project in the Lounge...
(i haven't done it yet, because I cannot get familiar with the strange OOP-coding for PB)
oh... and have a nice day.
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

No problem for me :wink: . I just saw that you posted it somewhere else
in the past the same way, so I thought I would tell you. It's somewhere
deep in the Lounge :P
And I think it's a well-known algorithm, too. (just forgot the name of it)
Athlon64 3700+, 1024MB Ram, Radeon X1600
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

Here's some slow code which generates a random number based on atmospheric noise. (connects to the net) :P. To actually use this, you would have to create some sort of cache or something..

Code: Select all

NewList rand()
Procedure.l generaterandom(total,min,max)
  Shared rand()
  Url.s = "http://www.random.org/cgi-bin/randnum?num=1&min="+Str(min)+"&max="+Str(max)+"&Col=5"
  Buffer.s=Space(2048)  
  INET_RELOAD.l=$80000000 
  hInet.l=0: hURL.l=0: Bytes.l=0 
  hInet = InternetOpen_("PB@INET", OpenType, #Null, #Null, 0) 
  For x = 1 To total
    hURL = InternetOpenUrl_(hInet, Url, #Null, 0, INET_RELOAD, 0)
    res.s= ""
    Repeat 
      InternetReadFile_(hURL, @Buffer, Len(Buffer), @Bytes) 
      If Bytes > 0 
        res.s = res + Left(Buffer, Bytes) 
      EndIf 
    Until Bytes = 0
    InternetCloseHandle_(hURL) 
    AddElement(rand())
    rand() = Val(Trim(res))
  Next x
  InternetCloseHandle_(hInet)
EndProcedure 
Procedure.l getrandom()
  Shared rand()
  SelectElement(rand(),Random(CountList(rand())-1))
  ProcedureReturn rand()
EndProcedure
generaterandom(5,0,5)
MessageRequester("Random",Str(getrandom()))
I love Purebasic.
Hatonastick
Enthusiast
Enthusiast
Posts: 149
Joined: Wed Apr 27, 2005 11:50 am
Location: Adelaide, Australia
Contact:

Post by Hatonastick »

You may want to check out this well known website which deals with RNGs:
http://random.mat.sbg.ac.at/generators/

One of the best known algorithms is the Mersenne Twister:
http://www.math.sci.hiroshima-u.ac.jp/% ... 937ar.html
Please forgive my poor English, I'm an Australian.
Post Reply