Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hello,
i have done some searching in this forum about generating random numbers, but didn't found any that answers exactly the problems that i am encountering now... they are about generating rondom numbers (in C language).
1) srand(time(NULL)); rand() will only generate a different sequence of random number after 1 second. are there any faster way to force the program to give a different random sequence, say 1/1000 second ?
2) actually i need to generate a sequence of random characters, say A, B and C. but if i need A to appear in the random sequence to generate with probabilty 1/5, B with probability 3/5 and C with 2/5. Theoretically, rand() will give the 3 chars an equal chance to appear.
for example, something like this is reasonable (10 chars): BCCBCCBABB
so, how can i do that? thanks a lot.
well, this is not a direct project question, but after much planning and simplification of a project problem. in order not to violate the 7484 (or some other code number?) assignment message, i 'd like to ask just hints and ideas.
thanks a whole bunch, any suggestions and answer would help me a lot !!! thanks.
wtk

I don't know C, so the only thing I can say about the 1st part is that in Java, you create a random number generator object, and that object has a next() method that returns a new random number each time. C wouldn't have something like that, but C++ might. I think I can help on the second part:
First of all, the probablilities 1/5, 2/5, and 3/5 don't all add up to 1. So those aren't valid numbers to use. For my example below, I'll use the probabilities A:3/10, B: 5/10, c:2/10
Most random number generators return a random number between 0 and 1. So to return A, B, and C with the probabilities you mentioned, do something like
x=random number
if x<.3
return 'A';else if x<.8
return 'B';else
return 'C';Thanks for just posting the portion of the project you're having trouble with. Both questions are legal by my standards.
Good luck,
-SN

A agree, both questions are legal by the "Law of 7404" ;)
You will find a very well structured random number generator library for C++ linked below...
"This is a C++ library for generating sequences of random numbers from a wide variety of distributions. It is particularly appropriate for the situation where one requires sequences of identically distributed random numbers since the set up time for each type of distribution is relatively long but it is efficient when generating each new random number. The library includes classes for generating random numbers from a number of distributions and is easily extended to be able to generate random numbers from almost any of the standard distributions."
http://www.robertnz.net/nr02doc.htm
IR

"Theoretically, rand() will give the 3 chars an equal chance to appear."
Just as a side, setting uneven probability automatically discludes randomness. To say you want a random number with probability is really an oxymoron. In fact, setting non-uniform probability is the converse of randomizing a sequence, in classical theory at least. Applying a bias discludes a sequence as random. You might say algorithms lean towards randomness or condition (sometimes called probability results, PR)- in that case rand() leans toward randomness.
Uniformity, the equal probability of all characters, is considered the best guard against probability-inclusion in your psuedo-random number generator (PRNG) and is the principle many PRNG algorithms use. In your example, you are not trying to create a random sequence but calculate and pick a possible sequence (you have conditions). A thin line outside of theory, to be sure.
You might try using rand, setting RAND_MAX to 2^x (where x is the maximum length of your character sequence). Then follow SN's example, divide the result up and set conditional ranges according to your probability.
netlib:
http://www.netlib.org/
http://www.netlib.org/random/The GNU Project also has some C code along these lines:
http://www.gnu.org/software/gsl/This RFC deals with security, but psuedo-random number generation is the real topic:
ftp://ftp.rfc-editor.org/in-notes/rfc1750.txtDonald Knuth's, The Art of Computer Programming, Volume 2: Seminumerical Algorithms contains some of the best discussions of number/random generation. You should read this anyway.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |