Solved

HELP w/ Random Numbers

Posted on 2003-10-22
16
188 Views
Last Modified: 2010-04-15
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!!
0
Comment
Question by:billjuniors
  • 5
  • 3
  • 2
  • +4
16 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9604405
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
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9604449
int num = random();

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

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

0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9604474
sorry, previous post not a single statement

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

0
 
LVL 6

Expert Comment

by:Ajar
ID: 9604584
bret   thats  ingenious...
though random should be rand:
return (((rand() % 5) + 1) << 1);

0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9604618
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
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9604679
> 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
 
LVL 45

Expert Comment

by:Kdo
ID: 9605874

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 6

Expert Comment

by:GaryFx
ID: 9606200
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
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9608119
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
 
LVL 45

Expert Comment

by:Kdo
ID: 9608162

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
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 30 total points
ID: 9608601
((random () %10) & 0xE) + 1  produces only odd numbers

0
 
LVL 45

Expert Comment

by:Kdo
ID: 9608723

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
 
LVL 1

Expert Comment

by:mattjsimps
ID: 9613243
if you get the number as an int in the range 1 - 5, you could just mulitply by 2
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9886751
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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert image to byte array 8 189
Directory does exist 19 144
distributed computing in Python 4 115
Problem to ASCII 1 166
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

920 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

14 Experts available now in Live!

Get 1:1 Help Now