• C

# Random numbers.

HI

can anyone tell me how to generate a random sign (meaning -1 or +1).
viz. each time i want either +1 or -1 randomly.

Cheers,
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
man srand.
Commented:
int gen_rand(void)
/* returns random number in range of 0 to 99 */
{
int n;
n=random(100);  /* n is random number in range of 0 - 99 */
return(n);
}
Commented:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int sign_rand ()
{
return rand() % 2 ? -1 : 1;
}

void main( void )
{
int i;

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

/* Display 10 numbers. */
for( i = 0;   i < 10;i++ )
printf( "  %6d\n", sign_rand() );
}

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
in this loop itself u can say

if (n > 50 ){
return(-1);
}
else {
return(1);
}
Author Commented:
hi

Thanks everyone for your answers,  I used ran1() to get a number number between 0 and 1 and returned(1) if > 0.5 else return(-1).

Now whose answer do I accept?

I will give it to jkr though shivsa also suggested exactly what i did.

Thanks again
pessi..
Commented:
Hi Pessi,

but if i suggest the one u are using. should not i get the credit too.
u can split the point if want.

thanks.
Author Commented:
hi

Hey I didnot know that points could be split.

I think they cannot be taken back can they?

pessi..
Commented:
yes. then can be.
u can post a message in CS(http://www.experts-exchange.com/Community_Support/).
and ask for the help in spliting point.

anyway thats cool.
u got it work.

-Shiv
DBACommented:

Hi pessi,

Although this question has been closed, let me offer another solution.

int GenerateRandom1 ()  /*  return +1 or -1 randomly  */
{
return ((int)clock() & 1 ? 1 : -1);
}

The random number generators used in most libraries produces a set of numbers that are GUARANTEED to have an approximate uniform distribution, given a large enough sample.  Mathematics has shown that this might be nice for some applications, but it doesn't produce "true" randomness since statistical oddities are automatically eliminated.

Toss 10 pennies into the air and let them fall.  Statistically, most "rolls" will see 4-6, 5-5, or 6-4 distributions of heads-tails.  But random number generaters tend to "gravitate toward the middle", meaning that rolls or 10-0, 9-1, 1-9, and 0-10 become more rare when using traditional random number generators.  In "real life", the 10-0 roll will occur about 1 time every 1024.  (Heads will occur 1 in 1024, so will tails, meaning that the odds of all heads or all tails is about 1 in 512.)  Add a wrapper around your random number generator and see how many times you have to call it to get the 1s in a row.  I'll bet it's a bunch!!!

One problem with the solution that I've posted is that if you repeatedly call it often enough, the system won't have had time to change the clock value.  You can get around this by using jkr's solution, but callins srand() within srand so that the randon number generator is reseeded with every call.

Good Luck,
Kent
Author Commented:
hi Kent

Thanks a lot for your suggestion.

Cheers,
Author Commented:
hello Kent

I used the following

int x,y;
for(i=0;i<=100;i++)
{
x = ((int)clock() & 1 ? 1 : -1);
y = clock();
printf("%d\t%d\n",x,y);
}

x is always -1 which forced me to calculate y and see that it is either 0 or 1000.
Am i doing a mistake?

Cheers,

DBACommented:

This is an anomaly in the clock resolution.  POSIX requires that CLOCKS_PER_SEC be 1,000,000 so that the clock() function will return a value that is nearly a microsecond clock.  Many systems, particularly older ones, have clock cycles slow enough that the value returned by clock() is incremented by a fairly large number.  The 500Mhz PC on my desk seems to have an increment of about 10000 meaning that the clock increments every 100 milliseconds.  However, the machine is clearly fast enough that it can query the clock more often than it increments.  I've run your small program several times and for any 1 execution, clock() always returned the same value.  I haven't tested any of my faster machines, but we already have a good explanation.

Further research indicates that there is an inconsistency in the meaning of the clock function.  Borland's documentation describes it as a real time clock so that successive calls to clock() can be used to determine the amount of time that has elapsed between events.  RH Linux describes the function as an approximation of processor time used by the program.

So it turns out that clock() isn't appropriate on all systems.

Apologies,
Kent
Author Commented:
hi

ok, got the point.
Any way thanks for passing the info.

Cheers,