Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 17

Discussione: random_in_c

  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2002
    Messaggi
    191

    random_in_c

    Sto realizzando un programma che gestisce il lotto!
    Il problema è che devo generare i numeri casuali, ma
    credo che in c o c++ non c'è la funzione random tipo in action script o java script!
    Come potri fare, ho pensato di generarmi dei numeri irrazionali e prendere particolari cifre ma non credo che vada bene!
    Possibile che un linguaggio potente come il c non ha questa funzione?
    Come potrie fare???
    Grazie in anticipo!!!


  2. #2

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2002
    Messaggi
    191
    Sotto dos!
    Grazie!!!!

  4. #4
    Originariamente inviato da goglol
    Sotto dos!
    Grazie!!!!
    dovrebbe essere sotto la libreria math
    la puoi includere con
    #include <math.h>

    usa randomize() per randomizzare
    e random() per ottenere un numero random


    senno prova la libreria dos.h

    chiedo scusa ma sono stanco e non ho un compilatore c sotto mano...

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2002
    Messaggi
    191
    Non voglio approfittare troppo della tua gentilezza,come devo fare, tipo a=random(91)
    che mi restituisce un numero da 0 a 90 ?
    Poi se dovesse uscire 0 ci metto una condizione, non c'è problema, ma
    avevo sentito che questa funzione in c non è molto affidabile!

  6. #6
    Originariamente inviato da goglol
    Non voglio approfittare troppo della tua gentilezza,come devo fare, tipo a=random(91)
    che mi restituisce un numero da 0 a 90 ?
    Poi se dovesse uscire 0 ci metto una condizione, non c'è problema, ma
    avevo sentito che questa funzione in c non è molto affidabile!
    mi sembra di si' ...
    sai mi confondo sempre con pascal..
    comunque e' affidabilissima, prima devi usare randomize();
    che in base al clock di sistema genera un vocabolario di numeri random
    diverso
    TRICK:
    se vuoi avere i numeri da 1 a 100
    fai random(100)+1!
    comunque prova...

  7. #7
    Utente di HTML.it
    Registrato dal
    Dec 2002
    Messaggi
    191
    Ok!
    ciao!

  8. #8
    Utente di HTML.it L'avatar di ChReAn
    Registrato dal
    Aug 2002
    Messaggi
    486
    #include <stdlib.h>

    int rand(void);

    void srand(unsigned int seed);

    DESCRIPTION
    The rand() function returns a pseudo-random integer
    between 0 and RAND_MAX.

    The srand() function sets its argument as the seed for a
    new sequence of pseudo-random integers to be returned by
    rand(). These sequences are repeatable by calling srand()
    with the same seed value.

    If no seed value is provided, the rand() function is auto-
    matically seeded with a value of 1.

    RETURN VALUE
    The rand() function returns a value between 0 and
    RAND_MAX. The srand() returns no value.

    NOTES
    The versions of rand() and srand() in the Linux C Library
    use the same random number generator as random() and sran-
    dom(), so the lower-order bits should be as random as the
    higher-order bits. However, on older rand() implementa-
    tions, the lower-order bits are much less random than the
    higher-order bits.

    In Numerical Recipes in C: The Art of Scientific Computing
    (William H. Press, Brian P. Flannery, Saul A. Teukolsky,
    William T. Vetterling; New York: Cambridge University
    Press, 1990 (1st ed, p. 207)), the following comments are
    made:
    "If you want to generate a random integer between 1
    and 10, you should always do it by

    j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
    and never by anything resembling

    j=1+((int) (1000000.0*rand()) % 10);

    (which uses lower-order bits)."

    Random-number generation is a complex topic. The Numeri-
    cal Recipes in C book (see reference above) provides an
    excellent discussion of practical random-number generation
    issues in Chapter 7 (Random Numbers).

    For a more theoretical discussion which also covers many
    practical issues in depth, please see Chapter 3 (Random
    Numbers) in Donald E. Knuth's The Art of Computer Program-
    ming, volume 2 (Seminumerical Algorithms), 2nd ed.; Read-
    ing, Massachusetts: Addison-Wesley Publishing Company,
    1981.


    E' una pagina man di linux, ma la funzione fa parte della libreria standard del C.
    ChReAn
    -------------------

    Slackware 9.1 powered

  9. #9
    Originariamente inviato da ChReAn
    #include <stdlib.h>

    int rand(void);

    void srand(unsigned int seed);

    DESCRIPTION
    The rand() function returns a pseudo-random integer
    between 0 and RAND_MAX.

    The srand() function sets its argument as the seed for a
    new sequence of pseudo-random integers to be returned by
    rand(). These sequences are repeatable by calling srand()
    with the same seed value.

    If no seed value is provided, the rand() function is auto-
    matically seeded with a value of 1.

    RETURN VALUE
    The rand() function returns a value between 0 and
    RAND_MAX. The srand() returns no value.

    NOTES
    The versions of rand() and srand() in the Linux C Library
    use the same random number generator as random() and sran-
    dom(), so the lower-order bits should be as random as the
    higher-order bits. However, on older rand() implementa-
    tions, the lower-order bits are much less random than the
    higher-order bits.

    In Numerical Recipes in C: The Art of Scientific Computing
    (William H. Press, Brian P. Flannery, Saul A. Teukolsky,
    William T. Vetterling; New York: Cambridge University
    Press, 1990 (1st ed, p. 207)), the following comments are
    made:
    "If you want to generate a random integer between 1
    and 10, you should always do it by

    j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
    and never by anything resembling

    j=1+((int) (1000000.0*rand()) % 10);

    (which uses lower-order bits)."

    Random-number generation is a complex topic. The Numeri-
    cal Recipes in C book (see reference above) provides an
    excellent discussion of practical random-number generation
    issues in Chapter 7 (Random Numbers).

    For a more theoretical discussion which also covers many
    practical issues in depth, please see Chapter 3 (Random
    Numbers) in Donald E. Knuth's The Art of Computer Program-
    ming, volume 2 (Seminumerical Algorithms), 2nd ed.; Read-
    ing, Massachusetts: Addison-Wesley Publishing Company,
    1981.


    E' una pagina man di linux, ma la funzione fa parte della libreria standard del C.
    ovviamente esistono altre 2:
    rand e srand..
    MAX_RAND dovrebbe inizialmente essere 65535..o na roba simile

  10. #10
    Utente di HTML.it L'avatar di ChReAn
    Registrato dal
    Aug 2002
    Messaggi
    486
    Editato, mi sa che ho capito male, sorry.
    ChReAn
    -------------------

    Slackware 9.1 powered

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.