Jump to content
Objectivism Online Forum

Reminder About The C Rand Command

Rate this topic


Prometheus98876

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?

Link to comment
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

Link to comment
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?

Link to comment
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
Link to comment
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.

Link to comment
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.

Link to comment
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... :lol:

Edited by Prometheus98876
Link to comment
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
Link to comment
Share on other sites

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

Could you please elaborate?

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.

Link to comment
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]

Link to comment
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.

Could you please elaborate?

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.

Assume now that my boss has already made up his mind about giving me a raise. This means that the determination of the outcome has been made; the causal mechanism leading to the future event is on its course; a person with enough knowledge about this fact knows that my boss will necessarily give me a raise. But, since I do not yet have access to this information, I do not at this point know that my boss will necessarily give me a raise. To my knowledge, it is still only a contingent outcome; it will switch to "necessary" when I find out about the causal mechanism that makes it necessary--but not sooner.

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.)

Link to comment
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.

Link to comment
Share on other sites

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...