GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Nov-2005, 12:49
Allie Allie is offline
New Member
 
Join Date: Nov 2005
Posts: 2
Allie is on a distinguished road

Need help generating a random number


I'm trying to generate a random number between 0.01 and 100 000 but everytime I test my program the same number is generated ,41.0998. Is that suppose to happen?

Here is part of my code:

CPP / C++ / C Code:
int main()
{
 float R4;
 
 R4 = 0.01 + ( rand() % 100000 ) ;
 printf (" %f\n " ,   R4) ;
}
  #2  
Old 08-Nov-2005, 13:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need help generating a random number


Quote:
Originally Posted by Allie
I'm trying to generate a random number between 0.01 and 100 000 but everytime I test my program the same number is generated ,41.0998. Is that suppose to happen?

Here is part of my code:

CPP / C++ / C Code:
int main()
{
 float R4;
 
 R4 = 0.01 + ( rand() % 100000 ) ;
 printf (" %f\n " ,   R4) ;
}
Wow! Someone that actually read the Guidelines first! Thank you!!!! And Welcome to GID.

Yes, that is supposed to happen the way you wrote your code. The random generator always starts at the same place unless you seed it first. You need to use the srand() function first (and only once). The typical way is to include time.h header and the line
CPP / C++ / C Code:
srand(time(NULL));
to initialize the generator. Also don't forget to include stdlib.h

I also note that you want a number between 0.01 and 100 000. rand() only returns 0-32767 so your range of 10 000 000 will be inaccurate. And your computation is off also.

First, switch to double.
Second, combine 2 randoms to get in the proper range
Third, multiply by your lowest value to get the proper magnitude
Forth, add your lowest value to prevent 0
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 08-Nov-2005, 15:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help generating a random number


Quote:
Originally Posted by WaltP
Wow!
I also note that you want a number between 0.01 and 100 000. rand() only returns 0-32767 so your range of 10 000 000 will be inaccurate.


According to the C Standards, the standard library function rand() returns a number in the range 0 to RAND_MAX, where RAND_MAX is an implementation-defined constant which is required to be at least 32767. Implementation-defined means that the programmers who supply the rand() function for a given compiler can make it anything they want (as long as it is at least 32767). Versions of GNU gcc that I am currently using have RAND_MAX = 2147483647.

There are two lessons here:

1. Don't make code that only works if RAND_MAX is 32767.

2. Realize that with some C compilers, there are only 32767 possible distinct values that you can get from rand(). This might be bad news, or it might not matter for your application, but it's always something to keep in mind.

One way to create random numbers for any range is to convert the integer return value from rand() to a floating point number between 0 and 1 and then multiply by whatever range you want.

So, if you want to generate numbers between 0 and 1000000, here is a way:

CPP / C++ / C Code:
/* 
 * find random numbers between 0 and range 
 * count how many are in the upper half of the range
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
  
  int i;
  double floatrand;
  int count = 0;
  double range = 1000000.0;
  double halfrange = range / 2.0;

  printf("RAND_MAX = %u\n", RAND_MAX); /* just for reference */

  srand(time(0));


  for (i = 0; i < 100; i++) {
    floatrand = range * (double)rand()/RAND_MAX;
    if (floatrand > halfrange) { 
      count++;
    }
  }
  printf("count = %d\n", count);

  return 0;
}

Here's a run when I used GNU gcc as a compiler:

Code:
RAND_MAX = 2147483647 count = 47

Here's a run from Borland bcc32

Code:
RAND_MAX = 32767 count = 49

Subsequent runs gave different count values, but count value was usually pretty close to the expected value.

Note that just I can create more-or-less uniformly distributed values between 0 and 1000000 even though there are actually only 32767 possible number values that I can get.

For example, I print the ten smallest numbers that I can get from this method:

CPP / C++ / C Code:
/* 
 * find random numbers between 0 and range 
 * count how many are in the upper half of the range
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
  
  int i;
  double x;
  double range = 1000000.0;

  printf("RAND_MAX = %u\n", RAND_MAX); /* just for reference */

  for (i = 0; i < 10; i++) {
    x = range * (double)i / RAND_MAX;
    printf("%3d: %f\n", i, x);
  }

  return 0;
}

With BOrland:

Code:
RAND_MAX = 32767 0: 0.000000 1: 30.518509 2: 61.037019 3: 91.555528 4: 122.074038 5: 152.592547 6: 183.111057 7: 213.629566 8: 244.148076 9: 274.666585

So, even though the numbers may be uniformly spaced over the range, I will never see two numbers closer together than roughly 30.5. Maybe that's OK, mabye not.

With GNU:
Code:
RAND_MAX = 2147483647 0: 0.000000 1: 0.000466 2: 0.000931 3: 0.001397 4: 0.001863 5: 0.002328 6: 0.002794 7: 0.003260 8: 0.003725 9: 0.004191

Here the numbers are spaced about .0005 apart. Maybe that's OK; maybe not. It depends.

In both cases, the values will be more-or-less uniformly distributed over the range [0, 1000000], but as you can see, the granularity is quite different.

For any given application one or both may be OK. For another application, neither may be OK. It's just something to keep in mind.

Note, that this says nothing about the quality of the rand() function itself (how "random" are the numbers that you get?) If rand() gives uniformly distributed numbers over its range, you can transform to any range you want, and the numbers are no more or no less "random" than before.


Regards,

Dave
  #4  
Old 09-Nov-2005, 01:55
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Need help generating a random number


Quote:
Originally Posted by davekw7x
granularity

Thank you for sending me to dictionary Dave, I really needed to find where I placed it

To the Original poster (actually, to anyone with interest), here is a good link that shows a very efficient "random" number generator : ISAAC cryptographic random number generator

Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #5  
Old 09-Nov-2005, 11:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help generating a random number


Quote:
Originally Posted by kobi_hikri
Thank you for sending me to dictionary Dave, I really needed to find where I placed it

Yeah; I was thinking of grains of salt versus grains of flour. Thus the term 'granularity'.

Quote:
Originally Posted by kobi_hikri
To the Original poster (actually, to anyone with interest), here is a good link that shows a very efficient "random" number generator : ISAAC cryptographic random number generator

The one thing that people sometimes ask is, "How random is rand()?" And in my own inscrutable way, I answer their question with a question, "How random to you want it to be?" Different compilers have different ways of implementing the standard library function rand(). The method is often selected for best efficiency on a given architecture, and may or may not be "random" enough for your purposes. (Nothing to do with granularity here, it has to do with the actual statistical probability distribution function of the chosen algorithm.)

A given implementation of rand(), might give sequences of individual integers that is very much like what you would expect from a truly random number generator with a uniform probability distribution, but may not be as successful in generating successive pairs of numbers (as you would need for a craps game simulation, for example).

That is, even though you could use rand() successfuly to simulate tossing a single die (range 1 to 6) a large number of times, the joint probability distribution for an experiment where you used two successive calls to rand() might not be as good a match for truly random tosses of two dice at a time.

That's why you see lots of different "random" number generator programs; you get to pick one that is random enough for your purposes.

For important research, the first thing to do is apply enough tests to your "random" number generator to give you a good feeling about its suitability for your application. (By comparing results with known results from problems of the same class as yours.)

Regards,

Dave

"Some people use statistics like a drunk uses a lamp post: for support rather than illumination."
---Mark Twain
  #6  
Old 09-Nov-2005, 15:22
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Need help generating a random number


Quote:
Originally Posted by davekw7x
"Some people use statistics like a drunk uses a lamp post: for support rather than illumination."
---Mark Twain

From where is that quote taken ?

Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #7  
Old 09-Nov-2005, 16:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help generating a random number


Quote:
Originally Posted by kobi_hikri
From where is that quote taken ?

Kobi.

Well, here's the thing: I am pretty sure Mark Twain wasn't the first to say it, and I know he wasn't the last. But if I attribute it to "Andrew Lang (1844-1912), Scottish Scholar and Man of Letters" it usually doesn't get a chuckle. (One sports page on the web attributed it to announcer Vin Scully, and I'm sure that old Vin said it too.)

In other words, I don't have an exact citation.

I have seen this particular quote lots of times here and there, but this part is actually the setup, not the punch line. Here's how the whole thing goes:

"Most people use statistics like a drunk uses a lamppost: for support instead
of illumination, when in reality they should be used like a dog uses a
lamppost, look it over carefully from all angles and then p... on it."

Interestingly, in his Autobiography he quotes Benjamin Disraeli, and the following anti-statistics quote is sometimes mis-attributed to Mark Twain.

"There are three kinds of lies --- lies, damn lies, and statistics"
---Benjamin Disraeli

Regards,

Dave
  #8  
Old 09-Nov-2005, 23:18
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Need help generating a random number


Hey Dave
You know, today I woke up on the left side and now, after reading this post I'm going to work with a smile.

Thanks,
Kobi.
__________________
It's actually a one time thing (it just happens alot).
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
random number generation issues Jonnyz007 C++ Forum 8 27-Oct-2005 20:13
Need Help with my Cards Program (C++) krisopotamus C++ Forum 2 06-Oct-2005 17:48
Function that returns a random number Tori C++ Forum 4 03-Nov-2004 21:48
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 21:25
Random() : Make each number onlu appear once NiXeN C++ Forum 3 13-Jan-2004 05:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:48.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.