PDA

View Full Version : [C] Passare a rand una variabile si puo??


@Eminem@
07-03-2011, 18:16
Dopo aver randomizazto a NULL vorre passare una variabile a rand in modo che generi un numero compreso tra 0 e la variabile passata..
esempio

int a,b;
printf("Inserisci un valore\n");
scanf("%d", &a);
b = rand () % a




si puo fare?? e se c'è un modo sarei grato se me lo spiegaste.
Grazie in anticipo!!

bobbytre
07-03-2011, 23:06
Questa è la mia funzione random



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

int random( int max )
{
static int init = 0;
if( init == 0)
{
init = 1;
srand( time( 0 ) );
}

return rand() % max ;
}

@Eminem@
07-03-2011, 23:42
Questa è la mia funzione random



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

int random( int max )
{
static int init = 0;
if( init == 0)
{
init = 1;
srand( time( 0 ) );
}

return rand() % max ;
}





Grazie 1000 finalmente ho finito il programma!!!!!
Ti ringrazio ancora!!!!!!
Molto utile!!

Hardware Upgrade Forum Database Error
Database Error Database error
The Hardware Upgrade Forum database has encountered a problem.

Please try the following:
  • Load the page again by clicking the Refresh button in your web browser.
  • Open the www.hwupgrade.it home page, then try to open another page.
  • Click the Back button to try another link.
The www.hwupgrade.it forum technical staff have been notified of the error, though you may contact them if the problem persists.
 
We apologise for any inconvenience.