[Rozdział 17] Zadanie domowe
Ostatnio zmodyfikowano 2016-01-02 17:45
domin123 Temat założony przez niniejszego użytkownika |
[Rozdział 17] Zadanie domowe » 2016-01-02 16:18:00 Cześć,mam problem z ostatnim zadaniem z 17Rozdziału kursu "Napisz funkcję, która losuje liczbę z przedziału, który zostanie podany poprzez argumenty funkcji.[...] ". Gdy uruchamiam program wyświetlają mi się cały czas te same cyfry.Proszę o pomoc.Oto mój kod: #include <iostream> #include <cstdlib> #include <ctime> using namespace std;
int cyfra( int start, int stop ) { srand( time( NULL ) ); int cyfra; return cyfra =(( rand() % start ) + stop ); } int main() { srand( time( NULL ) ); int start; std::cin >> start; int stop; std::cin >> stop; int ile = 20; do { std::cout << cyfra( start, stop ) << std::endl; ile--; } while( ile > 0 ); return 0; }
|
|
mimotego |
» 2016-01-02 16:36:34 Niepotrzebnie dwa razy wstawiasz srand( time( NULL ) ); wystarczy, że użyjesz tego w funkcji main. Zrobiłeś też błąd przy losowaniu, powinieneś zamienić miejscami start ze stopem. Pozdrawiam :) |
|
mateczek |
» 2016-01-02 16:52:52 int cyfra( int start, int stop ) { return( rand() %( stop - start ) ) + start; } |
|
domin123 Temat założony przez niniejszego użytkownika |
» 2016-01-02 17:45:20 Bardzo dziękuje ! :) |
|
« 1 » |