Solved

Random Numbers... argghhhhh!

Posted on 2004-09-03
16
260 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IE call stack entries where function call address is not in a module 4 62
Lambda for random numbers problem 7 106
thread-safe code in c++ 2 84
Least Squares Curve Fitting 4 60
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

911 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

20 Experts available now in Live!

Get 1:1 Help Now