Routine bas-niveau
fr - eng


genint_c - routine de calcul générateur de nombre entier aléatoire

Module

Paramètres

Contenu du fichier


/* genint_c subroutine
 * Random Integer Number Generator
 *
 * Copyright (C) 2007-2011 Alan Layec
 *
 * This file is part of modnumlib.
 *
 * modnumlib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * modnumlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with modnumlib; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/* REVISION HISTORY :
 * $Log$
 */

#include <stdlib.h>

#include "modnum_lib.h"

/* genint_c routine de calcul de mots entiers aléatoires
 *
 * Entrées :
 *  n     : dimension 1 de la matrice de sortie (scalaire)
 *  m     : dimension 2 de la matrice de sortie (scalaire)
 *  nbit  : longueur des mots binaires des éléments du vecteur de sorties (scalaire)
 *  typ   : type des générateurs binaires (scalaire)
 *          0 : génére 1 seul bit codé NRZ
 *          1 : génère 1 seul bit codé RZ, ou un mot non signé
 *          2 : génère un mot codé code complément à 2
 *
 * Sorties :
 *  y : pointeur sur double de sortie
 *
 * dépendances
 * math.h
 */

void genint_c(int *n,int *m,int *nbit,int *typ,double *y)
{
 /*déclaration des variables compteurs*/
 int i,j,k,l;

 switch (*typ)
 {
   /*Type 0 -> dédié signal NRZ*/
   case 0 :
   {
    for(l=0;l<(*m);l++) {
      for(i=0;i<(*n);i++) {
        y[l*(*n)+i] = (double) ((rand()&1)*2-1);
      }
    }
    break;
   }

   /*Type 1 -> mot non signé*/
   case 1 :
   {
    k = 1;
    k = k<<(*nbit);
    for(l=0;l<(*m);l++) {
      for(i=0;i<(*n);i++) {
        j = rand();
        y[l*(*n)+i] = (double) (j&(k-1));
      }
    }
    break;
   }

   /*Type 2 -> mot codé code complémént à 2*/
   case 2 :
   {
    for(l=0;l<(*m);l++) {
      for(i=0;i<(*n);i++) {
        j  = rand();
        j -= 2<<((*nbit)-2);
        j &= (2<<((*nbit)-1)) - 1;
        j -= 2<<((*nbit)-2);
        y[l*(*n)+i] = (double) j;
      }
    }
   }
   break;
 }
 return;
}

void geninti_c(int *n,int *m,int *nbit,int *typ,int *y)
{
 /*déclaration des variables compteurs*/
 int i,j,k,l;

 switch (*typ)
 {
   /*Type 0 -> dédié signal NRZ*/
   case 0 :
   {
    for(l=0;l<(*m);l++) {
      for(i=0;i<(*n);i++) {
        y[l*(*n)+i] = ((rand()&1)*2-1);
      }
    }
    break;
   }

   /*Type 1 -> mot non signé*/
   case 1 :
   {
    k = 1;
    k = k<<(*nbit);
    for(l=0;l<(*m);l++) {
      for(i=0;i<(*n);i++) {
        j = rand();
        y[l*(*n)+i] = (j&(k-1));
      }
    }
    break;
   }

   /*Type 2 -> mot codé code complémént à 2*/
   case 2 :
   {
    for(l=0;l<(*m);l++) {
      for(i=0;i<(*n);i++) {
        j  = rand();
        j -= 2<<((*nbit)-2);
        j &= (2<<((*nbit)-1)) - 1;
        j -= 2<<((*nbit)-2);
        y[l*(*n)+i] = j;
      }
    }
   }
   break;
 }
 return;
}

/* genintv_c routine de calcul de mots entiers aléatoires
 *
 * Entrées :
 * n   : taille du  vecteur de sortie (scalaire)
 * m   : longueur des mots binaires des éléments du vecteur de sorties (vecteur)
 * typ : type des générateurs binaires (vecteur)
 * (typ=0: génére 1 seul bit codé NRZ
 *    =1: génère 1 seul bit codé RZ, ou un mot non signé
 *    =2: génère un mot codé code complément à 2)
 * Sorties :
 * y : vecteur de sortie
 *
 * dépendances
 * math.h
 */
void genintv_c(int *n,int *m,int *typ,double *y)
{
 /*déclaration des variables compteurs*/
 int k,i,j;

 for(i=0;i<(*n);i++)
 {
  switch (typ[i])
  {
   /*Type 0 -> dédié signal NRZ*/
   case 0:
   {
    y[i]=(int)((rand()&1)*2-1);
    break;
   }
   /*Type 1 -> entier non signé*/
   case 1 :
   {
    k=1;
    k=k<<m[i];
    j=rand();
    y[i]=(j&(k-1));
    break;
   }
   /*Type 2 -> entier code complémént à 2*/
   case 2 :
   {
    j=rand();
    j -= 2<<(m[i]-2);
    j &= (2<<(m[i]-1)) - 1;
    j -= 2<<(m[i]-2);
    y[i]=j;
    break;
   }
   break;
  }
 }
 return;
}

Auteurs

A. Layec