Les Snippets

Connexion

Simuler la Fonction Rnd()

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 08/05/2006 11:47:31 et initié par Gobillot [Liste]
Vue : 9893
Catégorie(s) : Maths, Trucs & Astuces, Algorithme
Langages dispo pour ce code :
- VB6
- PHP 3, PHP 4, PHP 5



Langage : VB6
Date ajout : 08/05/2006
Posté par Gobillot [Liste]

 Const xA = 16598013
 Const xC = 12820163
 Const xM = 16777216
 Const xI = 602453
 Dim Seed As Long
 
 Private Sub Form_Load()
     Seed = 327680
 End Sub
 
 'simule la Fonction Rnd()
 Private Function Random() As Single
     Dim s As Long
     Dim x As Currency
     x = CCur(xA) * Seed + xC
     Seed = x - Int(x / xM) * xM
     Random = Seed / xM
 End Function
 
 'calcul le seed pour la valeur suivante
 Private Sub CalculSeed(r As Single)
     Seed = CCur(xM) * r
 End Sub
 
 'calcul le seed précédent
 Private Sub SeedPrecedent(s As Long)
     Dim x As Currency
     x = (s - xC) * CCur(xI)
     Seed = x - Int(x / xM) * xM
 End Sub
 
 'cherche la valeur correspondant à un seed
 Private Function ChercheSeed(s As Long) As Long
     Dim i As Long
     For i = 1 To 24143031
         Rnd -i
         CalculSeed Rnd
         SeedPrecedent Seed
         If Seed = s Then
            ChercheSeed = i
            Exit For
            End If
         If i And &H1FFFF Then DoEvents
         Next
     Beep
 End Function
 
 '//exemples d'utilisation:
 
 '//retrouve les valeurs initiales de la fonction Rnd()
 Private Sub Command1_Click()
     Dim i As Integer
     For i = 1 To 10
         MsgBox Random & vbCrLf & Rnd
         Next
 End Sub
 
 '//après un Randomize il faut recalculer le seed
 Private Sub Command2_Click()
     Randomize Timer
     CalculSeed Rnd
     MsgBox Random & vbCrLf & Rnd
 End Sub
 
 '//retrouve les valeurs dans l'ordre inverse
 Private Sub Command3_Click()
     Dim i     As Integer
     Dim r(10) As Single
     
     Randomize Timer
     For i = 1 To 10
         r(i) = Rnd
         Next
         
     CalculSeed Rnd
     SeedPrecedent Seed
     For i = 10 To 1 Step -1
         SeedPrecedent Seed
         MsgBox Random & vbCrLf & r(i)
         SeedPrecedent Seed
         Next
     CalculSeed Rnd
 
 End Sub
 
 '//initialise Rnd() avec un seed particulier
 Private Sub Command4_Click()
     Dim s As Long
     Dim R As Long
 
     s = 123456
     R = ChercheSeed(s)
     If R > 0 Then
        SeedPrecedent s
        MsgBox Random & vbCrLf & Rnd(-R)
        Rnd -R
        Seed = s
        MsgBox Random & vbCrLf & Rnd
        End If
 
 End Sub
 
 
Langage : PHP 3 , PHP 4 , PHP 5
Date ajout : 06/06/2007
Posté par coucou747 [Liste]
<?php
$val=1;
$val_=array(0xFFFFFF, 0, 0, 0);
$val__=0;
$val=$val_[3];

function randomBool(){
    global $val, $val_,  $val__;
    $val ^= $val_[$val__];
    $val_[$val__]=$val;
    $val__=($val__+1)%4;
    return $val;
}
function randomNum(){
    global $val;
    $val <<= 1;
    $val |= (($val >> 4) ^($val >> 1)) & 1;
    $val &= 15;
    return $val;
}
?>


Snippets en rapport avec : Aléatoire, Rnd, Random



Codes sources en rapport avec : Aléatoire, Rnd, Random

{Javascript / DHTML} NOMBRE ALÉATOIRE
Fonction réutilisable qui retourne un nombre aléatoire dans l'intervalle choisi.Inspiré de la foncti...

{Flash} DEPLACEMENT ALEATOIRE D' UN CLIP
bonjour à tous ! ceci est un petit code simple utilisant les bases de la fonction random (aléatoire)...

{Javascript / DHTML} RANDOM D'UN TABLEAU
Hello Every La demande se faisant sentir sur le forum, je vous livre une fonction permettant le ...

{Python} TESTER LA FIABILITÉE DE LA FONCTION RANDRANGE AVEC INTERFACE TYPE SISMOGRAPHE
Bon c'est un tableau qui a une echelle de 400 à -400 qui affiche la moyenne d'une liste de la foncti...

{Flash} UNE CLASSE "NUMBER RANDOM" PERMETTANT D'INFLUENCÉ LE RÉSULTAT GRÂCE A DES "TWEENS"
Classe Number générant des aléatoires modifiables selon le principe des "tween" de Robert Penner. P...

{Visual Basic, VB6, VB.NET, VB 2005} OBTENIR DE VRAIS NB ALEATOIRE
Est-il possible d'écrire un programme qui renvoi de vrais nombre aléatoire (imprévisible, équiprobab...

{Python} NOMBRE MYSTERE
Bon c'est un programme qui genere un nombre aleatoire. Ok c'est basique mais c'est tres commenter do...

{Delphi} GÉNÉRATEUR DE FICHIERS ALÉATOIRES : CLIENT DU SITE : HTTP://WWW.RANDOM.ORG/
Ce logiciel sert a créer des fichiers aléatoires à partir du site http://www.random.org/ Le prog. t...

{Visual Basic, VB6, VB.NET, VB 2005} TROUVER LE NOMBRE [VBS]
Voici un code que j'ai écrit vite fait Il s'agit en fait de trouver un nombre que l'ordinateur choi...

{PHP} UNE IMAGE AU HASARD PARMI TOUTES LES IMAGES D'UN RÉPERTOIRES PHP/JAVASCRIPT
se code explore le dossier choisie (dossier comtenant les images) puit dresse une listes des fichier...