:confused: Arrgh!!! Cant understand these sentence from this website of brickos programming:

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

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:

void main{
srandom(int x); 
long int k = random();
}
void main{
int x = 27;
void srandom(x); 
random();
}
void main{
random();
k = srandom(27);}

and many many possibilities... but still the compilation error comes out:

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.

>how can i achieve to have the program to randomly choose a number from 0 - 27?

#include <cstdlib>
#include <ctime>
#include <iostream>

int main()
{
  std::srand ( (unsigned)std::time ( 0 ) );

  int r = rand() % 28;

  std::cout<< r <<'\n';
}

Further details can be found here and here.

Thanks, Narue:

I have try:

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:

int x = 27;
		long int r = random(srandom ( (unsigned) x));

It was great, no more internal compiler problem but with another problem:

too many arguments to function 'long int random()'

can anyone tell me why?

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

// generate 10 random numbers
srandom( time(0) );
for(int i = 0; i < 10; i++)
   cout << random() << "\n";

Hey, thanks both of you.

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

Apparently, I miss up:

#include time.h

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

Start to know c++ better now. haha... :P

I mean

#include <ctime>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.