Objectivism Online Forum

# Reminder About The C Rand Command

Rate this topic

## Recommended Posts

I have forgotten how to program a random number generator into the C programming language, and my current textbooks are of no help here. Can someone remind me how I might get my program to generate a random number from 1 to 6?

##### Share on other sites

Technically, the term is pseudo-random. With that out of the way...

This link might help, though I have only skimmed it.

If any of the algorithms require the first pseudo-random number to be specified (the "seed"), the most common approach is to use the system time. If you system can return time in milliseconds, then you can to the following:

1) Get the time from the system. If it is 09:16:02.9206

2) That the millisecond portion and reverse the digits: 6029

Use that as you seed

The reason you reverse the digits is that you might be generating many random number sequences in quick succession. So, you might get two seed times as (say): 09:16:02.9206 and 09:16:02.9341

Rather than using 9206 and 9341 as successive seeds (which implies a kind of sequential pattern), you add to the psedo-randomness by reversing and using 6029 and 1439

##### Share on other sites

Ok, that helps abit. However it is fairly important that my outputs are more random than this allows. Other languages seem to handle this better. What about Visual Basic, does that produce more "random" numbers?

##### Share on other sites
Ok, that helps abit.  However it is fairly important that my outputs are more random than this allows.  Other languages seem  to handle this better.  What about Visual Basic, does that produce more "random" numbers?

This is the standard way of implementing a RNG, and the output will most likely suffice for your purposes.

Edited by Hal
##### Share on other sites

I will test it out, and if it gives approximetely accurate results then I will be happy, otherwise there is not much point putting the features in the program I am working on at the moment.

##### Share on other sites
However it is fairly important that my outputs are more random than this allows.
By "this" are you referring to the algorithms in the link I provided?

Other languages seem  to handle this better.
It is really not a language-related issue, more of an algorithm-related issue. Th C standard library probably has an implementation of one of these algorithms, as would Visual Basic.

From what I have read, to get more "randomness" that this, people use sensors that measure some "physical" phenomenon that has a "random" aspect (e.g. voltage on a line), and use that randomness as an input.

##### Share on other sites

No, I was just saying i will have to test how random such things are, and whether or not some languages have a easier way of handling things. I am consdering porting the whole thing to VB anyway.

##### Share on other sites

If you really want random & incomprehensible data use Kant(), not Rand()

##### Share on other sites
If you really want random & incomprehensible data use Kant(), not Rand()

Anyways, how will you test for randomness? As softwareNerd was leading to, functions like rand() and its counterparts in other languages will not give true random numbers and you could in fact, predict the returns you would get.

##### Share on other sites

You might be interested in Java's SecureRandom class.

##### Share on other sites
you could in fact, predict the returns you would get.

The question is not whether one could predict the results, but rather whether one does predict them. Randomness is an epistemological concept.

##### Share on other sites
The question is not whether one could predict the results, but rather whether one does predict them. Randomness is an epistemological concept.

##### Share on other sites

I am so seriously considering not bothering with the random number generation aspect of my program. It can do without it really. I mean, it is only for the purpose of an RPG character generator demo (proof of concept).

I can get the user to roll his own dice, it might give more reliable results at least. I might see if I can find a little random number generator applet I can use, if I can find a decent one.

However, I think I remember that I once made a program with VB what was adequate for this. I will try to remember all the commands and make my own little program out of it...

Nice play with word with the Kant thing...

Edited by Prometheus98876
##### Share on other sites
I am so seriously considering not bothering with the random number generation aspect of my program.  It can do without it really.  I mean, it is only for the purpose of an RPG character generator demo (proof of concept).

I can get the user to roll his own dice, it might give more reliable results at least.  I might see if I can find a little random number generator applet I can use, if I can find a decent one.

I honestly dont understand the problem. Just seed based on time to get a number, then use the modulus command to restrict it to whatever range you want (eg 1-6). This is probably how every program that simulates dice generates their numbers, including commercial applications - it is a standard method and perfectly sufficent for your purposes.

If VB has a rand() command in its standard library, I can almost guarantee that works by seeding system time in this way.

Edited by Hal
##### Share on other sites

Prometheus, Hal's right. Generating pseudo-random numbers is a breeze in most languages. Check the std. C library.

With the programming part laid to rest, let me take a shot at elaborating on the proposition that: randomness is an epistemological concept.

Things do not happen causelessly. Even things that appear to be "random" are not "uncaused". So, when we speak of randomness, we are referring to the unpredictability of something. Not to its "unknowable" nature but to an aspect of its nature that it is unknown to us: the aspect for which we see "no pattern" or for which we just do not have sufficient knowledge.

When it comes to computer-generated "pseudo random numbers", it usually suffices if the algorithm produces a stream of number that passes certain statistical tests for randomness. The simplest case would be to generate a sequence of single-digit numbers, where each has a equal probablity of appearing in the output, and in which there is no discernable pattern in which they follow each other.

For certain high-security applications, one might use special machines that sample an event where the "randomness" does not follow any known pattern. Each measure of such random aspect is expressed as a number.

##### Share on other sites
No, I was just saying i will have to test how random such things are, and whether or not some languages have a easier way of handling things.  I am consdering porting the whole thing to VB anyway.

You want easy, here's how to do it in python:

```import random
x = random.randint(1,6)[/code]```
##### Share on other sites
The question is not whether one could predict the results, but rather whether one does predict them. Randomness is an epistemological concept.

By "epistemological," I mean that it describes the nature of your knowledge about reality, not the nature of reality independent of your knowledge of it.

There are future events and states of affairs that we know will necessarily occur; for example, we know that the sun will necessarily rise next morning. These are outcomes that have already been determined; the causal mechanism that will eventually make them come true has already been set in place.

There are future outcomes that are contingent on other things happening; we know that they may or may not happen, depending on whether or not the causes that can lead to them are enacted. For instance, I know that my boss may or may not give me a raise. Whether he will actually give me a raise is up to him; he can choose to do so, but he may also choose not to do so. The outcome has not yet been determined in reality, and therefore it cannot yet be known to be determined in anyone's mind.

So contingency has a volitional element to it--the presence of a choice is one thing that can introduce a contingency--and also a cognitive element: the lack of information also leads to a contingency. The former has to do with reality itself, while the latter has to do with one's knowledge about reality.

Now consider the following scenario: I know the last random number my program has generated, and--since I myself have written the program--I also know the exact algorithm it uses to generate the next random number from the currently last one. If I wanted, I could take a calculator and execute the algorithm myself; then, I would know the number that my program would necessarily generate next. That is, there would exist a number N for which the proposition:

`My program will necessarily output N next`

was known by me to be true.

But I choose not to calculate the next number myself. This choice means there is no number N for which I know the above proposition to be true. To my knowledge, the outcome is still contingent for each and every N that my algorithm is capable of generating. (I know that there is exactly one number N that my program will output, and I also know that it is already determined which N it will output. The N has been established in reality--an immutable causal mechanism is in place to output one specific N--but as far as my knowledge of reality is concerned, it is still a question yet to be answered.)

So, if one has the option to turn an epistemological contingency into an epistemological necessity--an "I know it may or may not" into an "I know it will" or an "I know it will not"--but one chooses not to exercise that option, then one is still left with a contingency.

And it is precisely this contingency--this "may or may not"--that is one of the fundamental characteristics of a random outcome.

(BTW, the other fundamental characteristic--the lack of bias, or at least a well-defined probability distribution--presupposes a deterministic process. To know with certainty that a certain process will generate results without bias in the future, one must know that the process is governed by well-defined and unchanging laws.)

##### Share on other sites

OK, I have found an acceptable way to implement this in C++ as well as Visual Basic... so that is that out of the way

This thread has been useful though... we wandered abit more off topic into the theoritical side of things than I expected. But that is a good thing.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

• ### Topics

• 17
• 5
• 116
• 0
• 10
• 35
• 12
• 166
• 163
• 12
×
• Create New...