• C

HELP w/ Random Numbers

Write a single C statement that will print a random number from the set 2, 4, 6, 8, 10.  

I know how to write the rand() function for consecutive numbers and scaling those numbers into a certain range, but for a set??  Help please!

Thanks!!
billjuniorsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
keep the set sorted ...
generate random number as usual
bump the random number to to the next highest element of the set

e.g. suppose your random number is in r and set is in array set[]
i=0;
while ( i < MAX && r < set[i] )
    i++;

   r = set[i];

MAX is the cardianlity of the set
0
brettmjohnsonCommented:
int num = random();

while ((num & 7) > 4)      num >> 1;

return (((num & 7) + 1) << 1);

0
brettmjohnsonCommented:
sorry, previous post not a single statement

return (((random() % 5) + 1) << 1);

0
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

AjarCommented:
bret   thats  ingenious...
though random should be rand:
return (((rand() % 5) + 1) << 1);

0
dennis_georgeCommented:
Here set is an array which contains your random numbers

first you generate the random number and then map it your Set

int n = rand()%SET_MAX ;

return Set[n] ;


Dennis      
0
brettmjohnsonCommented:
> though random should be rand:

I explicitly used random() rather than rand() because rand() is a very poor
random number generator.  Extracts from the man pages:

RANDOM(3)               System Library Functions Manual              RANDOM(3)

NAME
     random, srandom, srandomdev, initstate, setstate - better random number
     generator; routines for changing generators

------------

RAND(3)                 System Library Functions Manual                RAND(3)

NAME
     rand, srand, sranddev, rand_r - bad random number generator
...

DESCRIPTION
     These interfaces are obsoleted by random(3).
0
Kent OlsenDBACommented:

There are several good suggestions already.  Here's another solution:

return ((random () %10) & 0xE);


Of course, if the result set is irregular, Sunnycode wins.  :)

Kent
0
GaryFxCommented:
re: num >> 1 and similar tricks for multiplying and dividing by two:

Most good compilers, with optimization turned on, will automatically convert i*2 or i/2 into an appropriate shift.  So keep your code lucid by writing num/2 or num * 2, and let the compiler do its job.

Gary
0
brettmjohnsonCommented:
Kdo,

((random () %10) & 0xE) can produce 0 and won't produce 10.

Notice that with  (((random() % 5) + 1) << 1),

(random() % 5) produces one of { 0, 1, 2, 3, 4 }

+ 1  maps that set to { 1, 2, 3, 4, 5 }

<< 1 (or * 2)  then produces { 2, 4, 6, 8, 10 }
 
0
Kent OlsenDBACommented:

That's what I get for trying to write application code.  ((random () %10) & 0xE) + 1  does the trick.....

The world SHOULD think relative 0 and in octal.  ;)


Kent
0
brettmjohnsonCommented:
((random () %10) & 0xE) + 1  produces only odd numbers

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenDBACommented:

See?  I'm still trying to think like an application's coder.

((random () %10) & 017) + 2


It makes sense once that "0xE" got removed.  Who ever heard of putting letters where numbers are supposed to go????

;)

0
mattjsimpsCommented:
if you get the number as an int in the range 1 - 5, you could just mulitply by 2
0
sunnycoderCommented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to brettmjohnson

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.