Solved

Random numbers.

Posted on 2003-11-28
13
896 Views
Last Modified: 2010-04-15
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,
prasad..
0
Comment
Question by:pessi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9838591
man srand.
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9838612
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);
}
0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 9838616
#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() );
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:shivsa
ID: 9838627
in this loop itself u can say

if (n > 50 ){
    return(-1);
}
else {
   return(1);
}
0
 

Author Comment

by:pessi
ID: 9838789
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..
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9838804
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.
0
 

Author Comment

by:pessi
ID: 9839252
hi

Hey I didnot know that points could be split.

I think they cannot be taken back can they?

pessi..
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9839324
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
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9844187

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
0
 

Author Comment

by:pessi
ID: 9849025
hi Kent

Thanks a lot for your suggestion.

Cheers,
prasad..
0
 

Author Comment

by:pessi
ID: 9849084
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,
prasad..





0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9849962
Hi prasad,

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
0
 

Author Comment

by:pessi
ID: 9849999
hi

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

Cheers,
prasad..
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question