Solved

Random Numbers... argghhhhh!

Posted on 2004-09-03
16
267 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:MrBarby
  • 7
  • 5
  • 4
16 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 11976062

rand() % UpperBound

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

-bcl
0
 
LVL 11

Accepted Solution

by:
bcladd earned 150 total points
ID: 11976086
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 11976127
Actually, you can increase the 'randomness' by using

int randomOnRange(int n) {

  srand( (unsigned)time( NULL ) );

  return rand() % n;
}
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:MrBarby
ID: 11976146
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
 
LVL 11

Expert Comment

by:bcladd
ID: 11976158
jkr's seeding with the time will start the sequence in a new spot.
0
 

Author Comment

by:MrBarby
ID: 11976161
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
 
LVL 86

Expert Comment

by:jkr
ID: 11976164
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
 
LVL 86

Expert Comment

by:jkr
ID: 11976179
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
 
LVL 11

Expert Comment

by:bcladd
ID: 11976181
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
 
LVL 11

Expert Comment

by:bcladd
ID: 11976192
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
 
LVL 86

Expert Comment

by:jkr
ID: 11976207
>> 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
 
LVL 11

Expert Comment

by:bcladd
ID: 11976234
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
 

Author Comment

by:MrBarby
ID: 11976260
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
 
LVL 11

Expert Comment

by:bcladd
ID: 11976298
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
 

Author Comment

by:MrBarby
ID: 11976368
cheers, thanks for your help :)
0
 
LVL 86

Expert Comment

by:jkr
ID: 11978645
>>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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now