Random Numbers... argghhhhh!

This is driving me insane!

I need a function that I can call that will provide me with a random integer from within a range... the function might be something like int randomNumber(int UpperBound);

help?

Thanks

MrBarby
MrBarbyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
bcladdConnect With a Mentor Commented:
So, you could write a function to do what the above says:

int randomOnRange(int n) {
  return rand() % n;
}

returns a number on the assymetric range [0-n)

The reason it is the poor man's version is that some implementations of rand() are not very random in their low-order bits. You probably don't care.

-bcl

0
 
bcladdCommented:

rand() % UpperBound

This is the poor man's version of what you want.

-bcl
0
 
jkrConnect With a Mentor Commented:
Actually, you can increase the 'randomness' by using

int randomOnRange(int n) {

  srand( (unsigned)time( NULL ) );

  return rand() % n;
}
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
MrBarbyAuthor Commented:
the main problem I have with rand() at the moment is the fact that the first time it is called it is the same every time... is there a way to stop this?
0
 
bcladdCommented:
jkr's seeding with the time will start the sequence in a new spot.
0
 
MrBarbyAuthor Commented:
sorry, jkr, didnt see you comment there - i think that might work, but since I will be calling this many times in a loop, time will not be suitable... is there something else I can use to set the seed?
0
 
jkrCommented:
Use 'srand()', as I wrote above - it initializes the randum number generator with a new value. E.g.

/* RAND.C: This program seeds the random-number generator
 * with the time, then displays 10 random integers.
 */

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

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", rand() );
}
0
 
jkrCommented:
You could call it once before entering the loop. Or (but, that's exotic)

int randomOnRange(int n) {

  srand( (unsigned)time( NULL ) * rand ());

  return rand() % n;
}
0
 
bcladdCommented:
Again:

srand((unsigned)time(NULL));

will seed the pseudo-random number generator so that it starts at a different place in the sequence. The sequence itself is fixed but very long so you don't have to worry about it.

You really want to seed the value once (jkr: Why would I want to seed the value every time? That makes little sense to me but I am happy to learn) at the beginning of the program execution.

-bcl
0
 
bcladdCommented:
If I were going to do this I would use a static flag:

int randomOnRange(int n) {
  static int init = 1;
  if (init) {
    srand((unsigned)time( NULL ));
    init = 0;
  }
  return rand() % n;
}

-bcl
0
 
jkrCommented:
>> Why would I want to seed the value every time?

That would increase the randomness a little bit, but in fact is counterproductive when called in short intervals.
0
 
bcladdCommented:
jkr-
Sorry to hijack another's question but how, exactly, would reseeding the random number generator with a series of values from an increasing sequence increase the randomness of the sequence? Can you point me at a reference? I glance through Knuth and I am not convinced. I also don't spend much of my time thinking about randomness so I stand ready to be amazed by this working.

-bcl
0
 
MrBarbyAuthor Commented:
Hmm... so are u supposed to set the seed just once or once every call?  Setting it every call produces the same number every time lol.
0
 
bcladdCommented:
Seeding everytime at a very fast interval will keep resetting the seed to the same value. That makes sense (if your loop is short enough and fast enough).

Periodically reseeding it should NOT increase the randomness according to my brain and quick reading right now. I remain open to being completely out of my mind.

-bcl
0
 
MrBarbyAuthor Commented:
cheers, thanks for your help :)
0
 
jkrCommented:
>>but how, exactly, would reseeding the random number generator with a series of values from an
>>increasing sequence increase the randomness of the sequence?

Not the randomness of the sequence that is initialized, but the exchange of sequences by using a different initializer is the idea. That will not lead to really random numbers, but by changing the initial vaue periodicaqlly, the numbers' will be more evenly distributed (IMHO, but fact is that my statistics readings date back by over 12 years :o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.