Cant understand random number generation

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 19
Reputation: Suraine is an unknown quantity at this point 
Solved Threads: 0
Suraine Suraine is offline Offline
Newbie Poster

Cant understand random number generation

 
0
  #1
Apr 8th, 2008
Arrgh!!! Cant understand these sentence from this website of brickos programming:

http://legos.sourceforge.net/HOWTO/x600.html

  1. 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.
  2.  

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:
  1. void main{
  2. srandom(int x);
  3. long int k = random();
  4. }
  1. void main{
  2. int x = 27;
  3. void srandom(x);
  4. random();
  5. }
  1. void main{
  2. random();
  3. k = srandom(27);}

and many many possibilities... but still the compilation error comes out:
  1. Internal compiler error.
  2. 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]
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Cant understand random number generation

 
0
  #2
Apr 8th, 2008
>how can i achieve to have the program to randomly choose a number from 0 - 27?
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. std::srand ( (unsigned)std::time ( 0 ) );
  8.  
  9. int r = rand() % 28;
  10.  
  11. std::cout<< r <<'\n';
  12. }
Further details can be found here and here.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Suraine is an unknown quantity at this point 
Solved Threads: 0
Suraine Suraine is offline Offline
Newbie Poster

Re: Cant understand random number generation

 
0
  #3
Apr 8th, 2008
Thanks, Narue:

I have try:
  1. int main()
  2. {
  3. std::srand ( (unsigned)std::time ( 0 ) );
  4. int r = rand() % 28;
  5.  
  6. std::cout<< r <<'\n';
  7. }

but the error remain the same.

So, i try out this:
  1. int x = 27;
  2. long int r = random(srandom ( (unsigned) x));
It was great, no more internal compiler problem but with another problem:
  1. too many arguments to function 'long int random()'
can anyone tell me why?

Headache.........
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Cant understand random number generation

 
0
  #4
Apr 8th, 2008
>>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
  1. // generate 10 random numbers
  2. srandom( time(0) );
  3. for(int i = 0; i < 10; i++)
  4. 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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Suraine is an unknown quantity at this point 
Solved Threads: 0
Suraine Suraine is offline Offline
Newbie Poster

Re: Cant understand random number generation

 
0
  #5
Apr 11th, 2008
Hey, thanks both of you.

I success in creating random numbers with method given by Narue.

Apparently, I miss up:
  1. #include time.h

What a shame, really feel silly to myself now...

Start to know c++ better now. haha...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Suraine is an unknown quantity at this point 
Solved Threads: 0
Suraine Suraine is offline Offline
Newbie Poster

Re: Cant understand random number generation

 
0
  #6
Apr 11th, 2008
I mean
  1. #include <ctime>
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC