954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to count integer from random number

hi,this is sandy patel.
i need simeone's help as soon as possible.
i discribe my problem as under:

Problem Statement:
This problem requires you to generate a series of random numbers.
There are many algorithms that have been developed for this purpose. One scheme generates
pseudorandom numbers by performing a calculation. The algorithm has been adapted to suit
this assignment. The method begins with an odd 4-digit integer, which is referred to as the
seed number. The seed is multiplied by the value (102 – 3). Using the lowest 4 digits of the
result (the residue) produces a new seed. Continuing this procedure produces a series of 4-
digit random numbers, with each new number used as the seed for the next number.
The following steps describe the algorithm.
Step 1: Enter an odd 4-digit integer seed number that is not divisible by 2 or 5.
Step 2: Multiply the seed number by 97.
Step 3: Extract the lower 4 digits of the result produced by step 2.
This is the procedure for Step 3.
3a: Divide the number by 104
3b: Keep the integer part of the result of step 3a.
3c: Multiply the result, in 3b, by 104.
3d: Subtract this result from the number produced by step 2.
Use this random number as the new seed.
Step 4: Repeat Steps 2 and 3 to generate as many random numbers as required.
Write a program that tests the effectiveness of this algorithm.
To do this we will generate a large number of pseudorandom integers, record them and
investigate the randomness of the numbers produced.
However, the algorithm above will produce numbers up to 9999. To check the randomness of
the output we would like to record the number of times each integer was generated but with
10000 numbers this would be a tedious process. Our task is to make a quick check possible
within the limits of our resources. The quickest way to present the results is on the screen but
it would be impractical to present all 10000 integers. To overcome this we will limit the test
to random numbers between 0 and 9. These can then be easily tabulated on the screen.
Your task then is to generate a large number (maximum 1000) of pseudorandom integers
between 0 and 9. You will use the algorithm above to generate the 4 digit pseudorandom
numbers and from each extract one of the 4 digits to produce a random number of 0-9.
Start by initialising 10 variables, used as counters, to zero. The counters will keep a sum of
the occurrence of each random digit generated. Print out the number of 0s, 1s, 2s etc. that
occurred and the percentage of time they occurred.

i have to make this type of table:

randomnumber count
0 22 or something.
1 ...
2 ...
3 ....

now,i don't know how do get the count from random number and as per my assigenment condition i can't use rand(0),srand(seed);
please help me as soon as possible,
thanks in advance.
bye.

civil1
Newbie Poster
6 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

http://www.daniweb.com/techtalkforums/announcement8-2.html
You're told the algorithm for implementing 'rand', so start by doing that

Say
int myRand();
Using the rules in the first half of your post.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

>>Extract the lower 4 digits of the result produced by step 2.

The easy way is to convert the number to a string then only use the last 4 digits.

int num = 12347;
char buf[10];
sprintf(buf,"%d",num);
int seed = atoi( &buf[1] );
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>>Extract the lower 4 digits of the result produced by step 2.

The easy way is to convert the number to a string then only use the last 4 digits.

int num = 12347;
char buf[10];
sprintf(buf,"%d",num);
int seed = atoi( &buf[1] );


The easier way is to divide by 10000 and take the remainder

int seed = num % 10000 ;
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

The easier way is to divide by 10000 and take the remainder

int seed = num % 10000 ;

OMG Yes that is a lot easier :*

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You