Solved

RANDOM(x): the SAME in ALL computers ?

Posted on 2000-02-17
23
252 Views
Last Modified: 2010-04-04
will

Randomseed:= 10;
a:= random(whatever)

be the same in all computers ?
I use randseed/random to encript/decript data.. and so I need to be sure that random will give me the same numbers in every computer.

bryan
0
Comment
Question by:bryan7
  • 5
  • 4
  • 3
  • +6
23 Comments
 

Expert Comment

by:topkapi
ID: 2532780
i don't think there's a guarentee for that.
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 25 total points
ID: 2532890
If you use randseed to initialise the random() function, you will get the same sequence of numbers on every computer.

There is a caveat: Borland can change the implementation of the random() function so that the sequence of numbers generated by an app compiled with D4 may not be the same as that generated by D5.

Cheers,

Raymond.
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2532894
I am sure that it will not. Random uses the system's clock to generate it's result. Obviously every system is not going to have the same processor or clock speed.
The Delphi help file only mentions differences between compiler versions, but I am sure that this hold true for different systems as well:



Because the implementation of the Random function may change between compiler versions, we do not recommend using Random for encryption or other purposes that require reproducible sequences of pseudo-random numbers.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2533190
DrDelphi: Perhaps you should read the help for randseed...

Cheers,

Raymond.
0
 
LVL 3

Author Comment

by:bryan7
ID: 2533432
well, I've tested the program in a few computers and it worked in all, I mean, I got the same seq. of numbers. but I also saw in delphi help something about non-reliability on it.. differences between compiler versions is not any problem.. I just want to be sure that for any circumstances the generated numbers will be different than expected..  I haven't got any problems with it until now.. but I wanted to be sure and hear all your oppinions..

bryan
0
 
LVL 3

Author Comment

by:bryan7
ID: 2533440
typos.. "the generated numbers will be
                    different than expected..  " meant "will NOT be diff. "
0
 
LVL 6

Expert Comment

by:edey
ID: 2533712
I'm afraid using random just isn't reliable.  You would be far better off to write your own random number generator (or reaserch someone else's, lord knows there are enough of 'em :)


GL
Mike
0
 
LVL 1

Expert Comment

by:MoonCalf
ID: 2534378
Dr Delphi - Random does *NOT* use the clock to produce random numbers.  It's a psuedo-random number created in relativity to the previous random number.  Raymond is right.  You are not.

MoonCalf
0
 
LVL 1

Expert Comment

by:MoonCalf
ID: 2534404
Dr Delphi - Random does *NOT* use the clock to produce random numbers.  It's a psuedo-random number created in relativity to the previous random number.  Raymond is right.  You are not.

MoonCalf
0
 
LVL 1

Expert Comment

by:MoonCalf
ID: 2534419
Dr Delphi - Random does *NOT* use the clock to produce random numbers.  It's a psuedo-random number created in relativity to the previous random number.  Raymond is right.  You are not.

MoonCalf
0
 
LVL 3

Author Comment

by:bryan7
ID: 2534744
so.. should I confide or not in randseed/random ?
it's quite important since I'm gonna use it in a big commercial app. about to be released..

thanks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:Jaymol
ID: 2534952
I personally agree with Mike (Edey) that writing your own random number generator is the best way to do this.  It's the only way you can guarantee the same results every time.  (With different versions as well!)

If you need help with that, let us know.  (Some people may be a bit arsey about doing that for 25 points, but maybe someone has one they could just give you without coding it.)

John.
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2535081
If you use this method it works Fine..

var
 num: integer;

begin
  Randomize;  //take this out, then yes its always the same......
    num:=random(100);
///This actually makes it work as "random" numbers, although computers doing real random numbers? nahhh

end;


Craig C.


0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2535105
Getting better ant not reading all the questions before i make a comment..... in encryption xor or?
0
 
LVL 1

Expert Comment

by:nrico
ID: 2535120
You could use some sort of extremely *sicko* formula that depends on the laster number given (Which would be randseed), something like this:

Const
  RandSeed:Integer=34750;

Function MyRandom:Integer;
Var
  Temp:Integer;
Begin
  Temp:=RandSeed*{ Sick formula goes here };
  RandSeed:=Temp;
  MyRandom:=Temp;
End;
0
 

Expert Comment

by:topkapi
ID: 2535183
facts:

1: random numbers in computers dont exist. random uses a algorithm to produce pseudo-random numbers (it has all the properties of a random number set, but is not truly random). this algorithm always works only one way.

2: if you start the same algorithm with the same input (random seed), it produces the same output (random number).

assumption:

3: the random algorithm works the same on every computer, every time (ie: it does not care what system it is running on, or what the system-time is). to know this for sure, you need to check the source of the used algorithm.

so you're real question comes down to:
- is it always the same algorithm that is used to generate random numbers?

attemted answer:
- if you compile your application once, and then distribute it (the same application), then the algorithm used will be the same everywhere, and the output is also the same

- if you compile diffrent version of the app in diffrent versions of delphi, the algorithm used may not be the same. so then your output is diffrent. (as DrDelphi showed)

does this make it clearer?

topkapi
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2535263
if you compile the code i gave u in delphi 1 2 3 and 4 or multiple copis in delphi 4 or 5 then... it will give different results everytime from the same exe.... is that not random... although yes it cant be totally random
0
 
LVL 6

Expert Comment

by:edey
ID: 2535421
I do have this, which I have based rng's on in the past, conversion should be trivial:

/*
C This random number generator originally appeared in "Toward a Universal
C Random Number Generator" by George Marsaglia and Arif Zaman.
C Florida State University Report: FSU-SCRI-87-50 (1987)
C
C It was later modified by F. James and published in "A Review of Pseudo-
C random Number Generators"
C
C THIS IS THE BEST KNOWN RANDOM NUMBER GENERATOR AVAILABLE.
C       (However, a newly discovered technique can yield
C         a period of 10^600. But that is still in the development stage.)
C
C It passes ALL of the tests for random number generators and has a period
C   of 2^144, is completely portable (gives bit identical results on all
C   machines with at least 24-bit mantissas in the floating point
C   representation).
C
C The algorithm is a combination of a Fibonacci sequence (with lags of 97
C   and 33, and operation "subtraction plus one, modulo one") and an
C   "arithmetic sequence" (using subtraction).
C========================================================================
This C language version was written by Jim Butler, and was based on a
FORTRAN program posted by David LaSalle of Florida State University.
*/

#include <stdio.h>
#include <stdlib.h>

#define TRUE -1
#define FALSE 0
#define boolean int

static void rmarin(int ij, int kl);
void ranmar(float rvec[], int len);

void main()
{
      float temp[101];
      int ij, kl, len, i;

      /* random seeds for the test case: */
      ij = 1802;
      kl = 9373;

      /* do the initialization */
      rmarin(ij,kl);
      
      /* generate 20,000 random numbers */
      len = 100;
      for (i=1; i<=200; i++)
            ranmar(temp, len);
            
/*
C If the random number generator is working properly, the next six random
C numbers should be:
C           6533892.0  14220222.0  7275067.0
C           6172232.0  8354498.0   10633180.0
*/
       len = 6;
       ranmar(temp,len);
       
       for (i=1; i<=6; i++)
             printf("%12.1f ", 4096.0*4096.0*temp[i]);
}

static float u[98], c, cd, cm;
static int i97, j97;
static boolean test = FALSE;

static void rmarin(ij,kl)
int ij, kl;
{
/*
C This is the initialization routine for the random number generator RANMAR()
C NOTE: The seed variables can have values between:    0 <= IJ <= 31328
C                                                      0 <= KL <= 30081
C The random number sequences created by these two seeds are of sufficient
C length to complete an entire calculation with. For example, if sveral
C different groups are working on different parts of the same calculation,
C each group could be assigned its own IJ seed. This would leave each group
C with 30000 choices for the second seed. That is to say, this random
C number generator can create 900 million different subsequences -- with
C each subsequence having a length of approximately 10^30.
C
C Use IJ = 1802 & KL = 9373 to test the random number generator. The
C subroutine RANMAR should be used to generate 20000 random numbers.
C Then display the next six random numbers generated multiplied by 4096*4096
C If the random number generator is working properly, the random numbers
C should be:
C           6533892.0  14220222.0  7275067.0
C           6172232.0  8354498.0   10633180.0
*/
      int i, j, k, l, ii, jj, m;
      float s, t;
      
      if (ij<0 || ij>31328 || kl<0 || kl>30081) {
            puts("The first random number seed must have a value between 0 and 31328.");
            puts("The second seed must have a value between 0 and 30081.");
            exit(1);
      }
      
      i = (ij/177)%177 + 2;
      j = ij%177 + 2;
      k = (kl/169)%178 + 1;
      l = kl%169;
      
      for (ii=1; ii<=97; ii++) {
            s = 0.0;
            t = 0.5;
            for (jj=1; jj<=24; jj++) {
                  m = (((i*j)%179)*k) % 179;
                  i = j;
                  j = k;
                  k = m;
                  l = (53*l + 1) % 169;
                  if ((l*m)%64 >= 32) s += t;
                  t *= 0.5;
            }
            u[ii] = s;
      }
      
      c = 362436.0 / 16777216.0;
      cd = 7654321.0 / 16777216.0;
      cm = 16777213.0 / 16777216.0;
      
      i97 = 97;
      j97 = 33;
      
      test = TRUE;
}

void ranmar(rvec, len)
float rvec[];      /* len random numbers are placed in rvec[1..len] */
int len;
/*
C This is the random number generator proposed by George Marsaglia in
C Florida State University Report: FSU-SCRI-87-50
C It was slightly modified by F. James to produce an array of pseudorandom
C numbers.
*/
{
      int ivec;
      float uni;
      
      if (test==FALSE) {
            puts("Call the init routine rmarin() before calling ranmar().");
            exit(2);
      }
      for (ivec=1; ivec<=len; ivec++) {
            uni = u[i97] - u[j97];
            if (uni < 0.0) uni += 1.0;
            u[i97] = uni;
            i97--;
            if (i97==0) i97 = 97;
            j97--;
            if (j97==0) j97 = 97;
            c -= cd;
            if (c<0.0) c += cm;
            uni -= c;
            if (uni<0.0) uni += 1.0;
            rvec[ivec] = uni;
      }
}



GL
Mike
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 2541639
This is getting a bit silly really isn't it!  All you need to do is make your own Random() function.  End of story!

John.
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2542499
True... agrees with Jaymol
0
 
LVL 6

Expert Comment

by:edey
ID: 2543306
hmmmm....
that would be what the above function is for


Gl
Mike
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2550395
Agrees with everyone :) hehe
0
 
LVL 3

Author Comment

by:bryan7
ID: 2626494
hi there, sorry I've been away for a few.. who should I give some points to ?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now