| | |
Cant understand random number generation
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 19
Reputation:
Solved Threads: 0
Arrgh!!! Cant understand these sentence from this website of brickos programming:http://legos.sourceforge.net/HOWTO/x600.html
C++ Syntax (Toggle Plain Text)
random() and srandom(int x) are now available in legOS. To use them, just call srandom(int x) at some point during your program startup. x is a "seed", which allows you to get the same sequence of numbers if you so desire (by passing the same seed) or to generate more truly "random" numbers by feeding in, for example, LIGHT_1. Be aware: unlike the "standard" implementation of these two functions, there is no "default" seed, so if you don't call srandom() at least once, random() will return the same number over and over again.
source code:
//! generate a random number /*! The random() function returns successive pseudo-random numbers * \return a random number in the range from 0 to RAND_MAX */ extern long int random(void); //! seed the random number generator /*! The srandom() function sets its argument as the seed for a new sequence * of pseudo-random integers to be returned by random(). These sequences * are repeatable by calling srandom() with the same seed value. If no * seed value is provided, the random() function is automatically seeded * with a value of 1. * \param seed * \return Nothing */ extern void srandom(unsigned int seed);
I have try:
C++ Syntax (Toggle Plain Text)
void main{ srandom(int x); long int k = random(); }
C++ Syntax (Toggle Plain Text)
void main{ int x = 27; void srandom(x); random(); }
C++ Syntax (Toggle Plain Text)
void main{ random(); k = srandom(27);}
and many many possibilities... but still the compilation error comes out:
C++ Syntax (Toggle Plain Text)
Internal compiler error. Please submit a full bug report.
Can anyone help me up?
how can i achieve to have the program to randomly choose a number from 0 - 27?
Head really wanna explode!!!
Thanks.
Last edited by Suraine; Apr 8th, 2008 at 2:07 pm. Reason: [/ code] to [/code]
>how can i achieve to have the program to randomly choose a number from 0 - 27?
Further details can be found here and here.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <ctime> #include <iostream> int main() { std::srand ( (unsigned)std::time ( 0 ) ); int r = rand() % 28; std::cout<< r <<'\n'; }
I'm here to prove you wrong.
•
•
Join Date: Jan 2008
Posts: 19
Reputation:
Solved Threads: 0
Thanks, Narue:
I have try:
but the error remain the same.
So, i try out this:
It was great, no more internal compiler problem but with another problem:
can anyone tell me why?
Headache.........
I have try:
C++ Syntax (Toggle Plain Text)
int main() { std::srand ( (unsigned)std::time ( 0 ) ); int r = rand() % 28; std::cout<< r <<'\n'; }
but the error remain the same.
So, i try out this:
C++ Syntax (Toggle Plain Text)
int x = 27; long int r = random(srandom ( (unsigned) x));
C++ Syntax (Toggle Plain Text)
too many arguments to function 'long int random()'
Headache.........
>>long int r = random(srandom ( (unsigned) x));int x = 27;
No No NO. You can not do that. The intent of srandom() is just like the standard C library function srand(), which is to be called only once during the lifetime of the program. After that random() can be called as often as you like to generate random numbers
No No NO. You can not do that. The intent of srandom() is just like the standard C library function srand(), which is to be called only once during the lifetime of the program. After that random() can be called as often as you like to generate random numbers
C++ Syntax (Toggle Plain Text)
// generate 10 random numbers srandom( time(0) ); for(int i = 0; i < 10; i++) cout << random() << "\n";
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 19
Reputation:
Solved Threads: 0
Hey, thanks both of you.
I success in creating random numbers with method given by Narue.
Apparently, I miss up:
What a shame, really feel silly to myself now...
Start to know c++ better now. haha...
I success in creating random numbers with method given by Narue.
Apparently, I miss up:
C++ Syntax (Toggle Plain Text)
#include time.h
What a shame, really feel silly to myself now...
Start to know c++ better now. haha...
•
•
Join Date: Jan 2008
Posts: 19
Reputation:
Solved Threads: 0
I mean
C++ Syntax (Toggle Plain Text)
#include <ctime>
![]() |
Similar Threads
- password generation (C)
- Random number generation (C)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- algorithm help (Game Development)
- Random number Generation (C)
- Random Number (C++)
- need help, need to generate 10 random numbers, such that their sum is less than 1 (C)
Other Threads in the C++ Forum
- Previous Thread: Two loop run together at a time
- Next Thread: Dynamic array of structures
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






