Re: random number generator
From: G_cowboy (vvosen_at_cpinternet.com)
Date: 02/11/04
- Next message: Richard Heathfield: "Re: Borland 3.1"
- Previous message: G_cowboy: "Re: Beginner for C++ Help..."
- In reply to: Jerry Coffin: "Re: random number generator"
- Next in thread: Karl Heinz Buchegger: "Re: random number generator"
- Reply: Karl Heinz Buchegger: "Re: random number generator"
- Reply: Jerry Coffin: "Re: random number generator"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 11 Feb 2004 00:30:45 -0500
Jerry Coffin wrote:
> In article <102jfimbadu52e4@corp.supernews.com>, vvosen@cpinternet.com
> says...
>> what library would I use to generate a random number within a range
>> and what would be the function call?
>
> Code for this has been posted fairly recently; fortunately, it's short
> enough that I guess one more time wouldn't hurt:
>
> #include <stdlib.h>
>
> int rand_lim(int limit) {
> /* return a random number between 0 and limit inclusive.
> */
>
> int divisor = RAND_MAX/(limit+1);
> int retval;
>
> do {
> retval = rand() / divisor;
> } while (retval > limit);
>
> return retval;
> }
>
>> I'm building this data base file
>> that contains filenames for display but I want them to be randomly
>> selected
>> as well as possible. I plan on storing the filenames within an array
>> and would like to present the files(pics) in a sequence while keeping
>> track of how many times they were displayed so that no file was displayed
>> twice. Yea, I've got my work cut out for me and I'm chewing on c++.
>
> For this job, you might want to start with something like a vector of
> strings, and use random_shuffle to put them in a random order. Then you
> can simply keep track of the current spot on the vector, and when you
> need to show another randomly-chosen name, you just use the next one in
> the vector.
>
Problem with shuffling is that the filenames have pairs so shuffling would
just confuse me and the program wouldn't be able to keep track of any
corrolaries that I need to work with. I'm needing the display random, not
the file structure. As to the code you included, thanks anyway, but I'd
rather try figuring out how srand() works before I try that.
- Next message: Richard Heathfield: "Re: Borland 3.1"
- Previous message: G_cowboy: "Re: Beginner for C++ Help..."
- In reply to: Jerry Coffin: "Re: random number generator"
- Next in thread: Karl Heinz Buchegger: "Re: random number generator"
- Reply: Karl Heinz Buchegger: "Re: random number generator"
- Reply: Jerry Coffin: "Re: random number generator"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|