2009-02-11 21 views

risposta

16

Il Delphi PRNG è deterministico linear congruential generator con 134.775.813 come a e 1 come c, e restituendo le alte 32 bit per i numeri gamma limitata. Ecco un'implementazione in C# che restituisce gli stessi valori come Delphi:

using System; 

class DelphiRandom 
{ 
    int _seed; 

    public DelphiRandom(int seed) 
    { 
     _seed = seed; 
    } 

    int GetNext() // note: returns negative numbers too 
    { 
     _seed = _seed * 0x08088405 + 1; 
     return _seed; 
    } 

    public int Next(int maxValue) 
    { 
     ulong result = (ulong) (uint) GetNext() * (ulong) (uint) maxValue; 
     return (int) (result >> 32); 
    } 
} 

class App 
{ 
    static void Main() 
    { 
     DelphiRandom r = new DelphiRandom(42); 
     for (int i = 0; i < 10; ++i) 
      Console.WriteLine(r.Next(100)); 
    } 
} 
7

Certamente no, perché usano RNG diversi. Potresti forse usare un RNG dall'API di Windows, creare il tuo RNG personale o usare una libreria RNG per raggiungere questo obiettivo.

Un altro modo per assicurarsi che il tuo RNG crei gli stessi numeri per un dato seme sarebbe quello di scrivere una DLL e usarla sia per Delphi che per C#.

A proposito, se si desidera codificare un RNG da soli, Wikipedia è un buon punto di partenza per ottenere i nomi di alcuni generatori abituali. Dopo aver finito, dovresti eseguirlo tramite lo statistical test per assicurarti che sia abbastanza "casuale" per te.

3

Solo alcuni risultati utilizzando Delphi 2009 e il primo 10 di ogni seme:

Seed: 0, result: 0, 8, 219, 51, 69, 171, 81, 41, 94, 108 
Seed: 1, result: 8, 219, 51, 69, 171, 81, 41, 94, 108, 20 
Seed: 2, result: 16, 176, 138, 87, 17, 246, 1, 148, 122, 188 
Seed: 3, result: 24, 132, 225, 105, 119, 156, 216, 202, 135, 100 
Seed: 4, result: 32, 89, 57, 123, 221, 66, 176, 0, 149, 13 
Seed: 5, result: 40, 45, 145, 141, 67, 231, 136, 54, 163, 180 
Seed: 6, result: 48, 2, 232, 159, 169, 141, 96, 108, 176, 92 
Seed: 7, result: 56, 213, 64, 177, 16, 51, 56, 161, 190, 5 
Seed: 8, result: 64, 170, 151, 195, 118, 216, 16, 215, 203, 172 
Seed: 9, result: 72, 127, 238, 213, 219, 126, 231, 14, 217, 84 
Seed: 10, result: 80, 83, 70, 231, 66, 36, 191, 67, 231, 252 
Seed: 11, result: 88, 40, 157, 248, 168, 201, 151, 121, 244, 164 
Seed: 12, result: 96, 251, 244, 11, 14, 111, 111, 175, 3, 76 
Seed: 13, result: 104, 208, 76, 29, 116, 21, 71, 228, 17, 244 
Seed: 14, result: 112, 164, 163, 47, 218, 186, 31, 27, 30, 156 
Seed: 15, result: 120, 121, 250, 65, 64, 96, 246, 81, 44, 69 
Seed: 16, result: 128, 78, 83, 83, 166, 6, 206, 134, 57, 236 
Seed: 17, result: 136, 34, 170, 101, 13, 171, 166, 188, 71, 148 
Seed: 18, result: 144, 246, 2, 119, 114, 81, 126, 242, 85, 61 
Seed: 19, result: 152, 202, 89, 137, 216, 246, 86, 40, 98, 228 
Seed: 20, result: 160, 159, 176, 155, 63, 156, 46, 94, 112, 140 
Seed: 21, result: 168, 115, 8, 173, 164, 66, 6, 148, 126, 53 
Seed: 22, result: 176, 72, 95, 191, 11, 231, 221, 201, 139, 220 
Seed: 23, result: 184, 29, 182, 209, 113, 141, 181, 0, 153, 132 
Seed: 24, result: 192, 240, 14, 227, 214, 51, 141, 54, 166, 45 
Seed: 25, result: 200, 197, 101, 245, 61, 216, 101, 107, 180, 212 

vedo un modello ;-).

+0

È questo dal C# o Delphi RNG? – schnaader

+0

Delphi, non ha un ambiente delphi. Ma l'ho corretto. Grazie. –

+0

Sì, Delphi 5 in effetti dà lo stesso. Testato anche qui. Indovina, ecco perché dovresti buttare via i primi valori casuali per la maggior parte degli RNG. – schnaader

1

Per gli sviluppatori Java

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package org.delphi; 

/** 
* 
* @author ulmum 
* Using Code for C# from Barry Kelly http://stackoverflow.com/users/3712/barry-kelly 
*/ 
class DelphiRandom { 

    int _seed; 

    public DelphiRandom(int seed) { 
     _seed = seed; 
    } 

    int GetNext() // note: returns negative numbers too 
    { 
     _seed = _seed * 0x08088405 + 1; 
     return _seed; 
    } 

    public int Next(int maxValue) { 
     long result = (long) (int) GetNext() * (long) (int) maxValue; 
     return ((int) (result >> 32) & 0xff); //Here Prevent Negative Numbers 
    } 
} 

class App { 

    public static void main(String[] args) { 
     DelphiRandom r = new DelphiRandom(0); 
     for (int i = 0; i < 10; ++i) { 
      System.out.println(r.Next(256)); 
     } 
    } 
} 
Problemi correlati