|
-
November 8th, 2002, 02:39 AM
#1
how to produce a 6 bit random integer
how to produce a 6 bit random integer. i means how to control the bits
e.g.
213432,
312356,
,,,,,
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
main()
{
int i;
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
printf("new line \n");
}
-
November 8th, 2002, 03:11 AM
#2
bits, digits, base, or?
6 bits:
rand() % 64
but 213423 is six digts? base 6? I'm sorry, but I don't quite understand... If you post a clarification, I would love to try to help!
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
November 8th, 2002, 11:51 AM
#3
Do you mean a six digits random number? Otherwise, use bit shift << operator. An integer is 32-bits in Windows.
Kuphryn
-
November 8th, 2002, 03:29 PM
#4
-----------------------------------------------
how to produce a 6 bit random intege
----------------------------------------------------
Assuming u need 6 digit number
The rand functions returns a number between 0 to RAND_MAX
By standard the RAND_MAX is implementation dependant
for Microsoft compiler this is 0x7fff
So therotically u can not get number bigger than 0x7fff in VC++ by random method
kuphryn provides shifting solution which is going to give u six digit number but the last digit always going to be same
Vinod
-
November 9th, 2002, 02:39 PM
#5
If you are looking at a more reliable random stream of data, you could try the implementations in OpenSSL or Crypto++.
-
November 9th, 2002, 08:32 PM
#6
i think for a 6 bit one galathaea's answer would be correct
for a 6 digit one you could try something like
1000*(rand()%1000)+(rand()%1000)
of course initializing first with srand
-
November 10th, 2002, 01:19 AM
#7
Just to clarify s. roelants' answer
Problem with rand() on some machines is that the range is from 0 to RAND_MAX. On a lot of compilers, this happens to be 32767. What is being done in the solution is to get two pseudo random 3 digit numbers and concatenate them.
Succinct is verbose for terse
-
November 10th, 2002, 11:53 AM
#8
When I make random numbers that way, I find that the fewer digits you take, the more "random" they are. I once made a short little function that gives you a random number of n digits. Here's the gist of it (although I'm not sure it will compile because it will probably have typos here):
Code:
int randomnum(int a)/*a is the number of digits you want it to have*/
{
int b=0;
for(int c=0;c<a;c++)
{
b*=10;
b+=rand()%10;
}
return b;
}
You could simply use this with a=6.
SolarFlare
Those who cling to life die and those who defy death live. -Sun Tzu
cout << endl;
return 0;
}
-
November 10th, 2002, 02:46 PM
#9
concerning random character of concats
Solarflare is absolutely correct. Since RAND_MAX is not an exponent of ten (on any machine I've seen, but generally its at least not guaranteed to be), there will be some numbers that will have a greater likelihood of occuring than others.
For example: take RAND_MAX = 32767
taking % 1000 gives
33 possibilities to get #s 0 - 767
32 possibilities to get #s 768 - 999
but taking % 10 gives
3277 posibilities to get #s 0 - 7
3276 possibilities to get #s 8, 9
By choosing a smaller modulus to concatenate makes the likelihood ratios closer to 1:1, thus giving a better random character. Of course, the best way is to just exclude the numbers from RAND_MAX down to RAND_MAX - RAND_MAX % N (N = 10, 1000, etc) as contributing to the asymmetry by taking another rand if such occurs (repeat as necessary). Then, its a tradeoff between rand returns to exclude and number of calls of rand to concatenate...
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
November 10th, 2002, 05:31 PM
#10
If you take rand()%(2 to some power less than 16) then you will get a truly random number. Even rand()%10 has some (ignorable) flaws because it's not a power of two. But it's easier to work with .
I suppose you could have
Code:
int randomnum(int a)/*a is the number of digits you want it to have*/
{
int b=0;
for(int c=0;c<a;c++)
{
b*=10;
b+=rand()%8;
b+=rand()%2;
}
return b;
}
and it would be even better.
SolarFlare
Those who cling to life die and those who defy death live. -Sun Tzu
cout << endl;
return 0;
}
-
November 10th, 2002, 07:10 PM
#11
oops!
I don't know how to delicately do this... but there are a couple of flaws in SolarFlare's last post...
The rands will only give an integer from 0 to 8, not 0 to 9 (0-7 plus 0, 1) and also, the endpoints 0, 8 will have less of a chance (only one possibility each) of being picked than 1-7 (2 possible ways each). But I absolutely agree with the opinion that, for around the house type didn't get out of my pjs today random number generation, the %10 solution's side effects are ignorable.
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
November 10th, 2002, 10:12 PM
#12
Re: oops!
Originally posted by galathaea
I don't know how to delicately do this... but there are a couple of flaws in SolarFlare's last post...
NOOOO!!!!! now I must commit ritual suicide!!!! I'm a failure!!!!!
I mean, I just forgot to check over my logic 
Hmmm... I guess there's no easy way, then, to get a truly random decimal number unless you have something like this:
Code:
b=10;
while(b>=10)
{
b=rand()%16;
}
but it's not very efficient.
SolarFlare
Those who cling to life die and those who defy death live. -Sun Tzu
cout << endl;
return 0;
}
-
November 10th, 2002, 10:17 PM
#13
Or, to increase the likelihood that it won't have to loop more than once:
Code:
do
{
d=rand()%512;
b=d%10;
}while(d>=510);
Now it will only repeat the loop once every 256 times you run it.
SolarFlare
Those who cling to life die and those who defy death live. -Sun Tzu
cout << endl;
return 0;
}
-
November 14th, 2002, 02:34 PM
#14
First off, using the line:
rand()%(maxval + 1)
to produce a random integer between 0 and maxval *generally* does not produce uniformly random results. As solarflare pointed out, for most implementations, if maxval is ((power of 2) - 1), then this will produce uniformly random results assuming rand() produces uniformly random results.
I have worked in the science and engineering fields for a while and do not trust most compilers' implementations of rand(). I personally recommend reading chapter 7 of Numerical Recipes for C. This has good algorithms for random numbers and some suggestions for producing the very results you wanted.
PDF versions of Numerical Recipes for C are available online at:
http://www.ulib.org/webRoot/Books/Nu.../bookcpdf.html
Hope this helps!
- Kevin
-
November 14th, 2002, 04:55 PM
#15
Using modular or divide operation to get random number of a specific range is extremely slow and gives you none-uniform distribution. So it is not recommended.
Here is a function that is fast, and gives you uniform distribution and is completely random:
Code:
int GetRand()
{
static int nPrevious = 0;
if (((nPrevious&0xff) ^ (nPrevious>>12)) == 0)
{
// Make sure we re-seed once in a random while.
srand(nPrevious ^ GetTickCount());
}
do {
int tmp = rand();
nPrevious = ((tmp<<5) - tmp) ^ (nPrevious>>1);
} while (nPrevious > 999999 || nPrevious < 100000);
return nPrevious;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|