• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Generating Random Numbers (Need help ASAP!)

Joined
May 16, 2008
Messages
1,258 (0.20/day)
Location
North Carolina
Working on a math problem.

  • First, I need to generate a random integer from 1 to 6. I need to keep doing this until I get the number 3. I need to record how many numbers had to be generated.
  • Second, I need to generate random integers from 1 to 6 until I have picked every number once. Again, I need to know how many numbers were generated.
I need to do each situation a certain number of times (which is up to me) to get an average of how many numbers had to be generated in order to meet the condition.

Being the overachiever that I am, I'd like to generate an absurd number of trials. So I want to do the first situation a huge number of times and then get the average. Same for situation 2. Is there any way I could use my computer to do this? My Q6600 is on Windows and my 2GHz Core Duo laptop runs Ubuntu.
 
Program an app. I can't make it for you unless you installed Mono (which I don't know how good/current it is).

#1 will be completed in 1~11 attempts.

#2 will be completed in 6~35 attempts.

It depends on the time, literally (random numbers are generated from the system clock).


Edit: Let me guess, this has something to do with the rolling of a dice? Rolling dice are based on physics; randomization algorithms are based on binary manipulation.
 
Last edited:
I'm not much of a programmer. I'm decent at Basic like stuff and I know a bit of Java. So unless it's pretty simple, it's probably over my head.

I can get Mono installed. Is there a particular version you want? Windows or Linux or does it matter? (Mono is cross platform, no?)

When you say it depends on time, does that mean that the more times passes, the more numbers will be generated? Or will the numbers be more random..?


Edit: Actually no dice this time. We're tryna find out how many cereal boxes it takes in order to get the red toy guitar. (which is color #3) But you could change the problem so that it uses dice though.

How is the physics vs binary manipulation applicable thing to me? (ie what practical difference does it make)

Thanks.
 
The outcome of rolling a physical dice depends upon environmental variables in your physical surroundings. Random algorithms, that generate pseudo-random numbers, do not take any physical properties into play. The outcomes of each will differ.

You can do this easily in basic.

create a counter variable and set it to zero
Do a while loop until the terminating condition is met (eg. random number = 3)
Increment the counter
Generate a random number between 1 and 6
If the condition is met, break out of the loop.
If not, let the loop run again.
The counter will hold the number of attempts.

The second one is a little more complex as you will have to store which numbers have been "hit" and re-roll if you hit a number twice.

Since there are no requirements on how many times you have to do this to create an average, it may quicker to just roll dice.

That is, of course, up you you and we wil lend a hand if need be.
We will not however, code the whole assignment for you.
So chop chop grasshopper, be an overachiever and get to coding !!! ;)
 
.NET Framework (Windows) ~= Mono Project (Linux)

Random calculations are based on the current time. That means, every second will generate a different set of random numbers.


Because random number generators in computers won't match the physics that judge how a dice will roll. I was thinking maybe you wanted to figure out what's the "luckiest" number to guess in gambling, for instance, which wouldn't be accurate to use a computer for.


I can give you a single answer to #1 and #2 if you want.


Edit: Here's three:
#1: 6
#2: 19

#1: 7
#2: 16

#1: 8
#2: 8


This won't acurately predict that you'll get that toy in the ceral box. It depends on the order they put them in the cereal box, how they separate them for delievery, how the shelf stockers place incoming items on the shelves, and how many were sold since the most recent delivery. Knowing all that, you could probably find the toy on the first attempt.
 
Last edited:
Maybe I should explain more. The teacher expects us to either use a random number chart in our math books or use our calculator to generate random numbers. He also expects us to do anywhere from 5-10 trials and then get an average from that. I can do that no prob.

But being me, I thought, "Wouldn't you get a much more accurate average if you had more trials? Say 1000, or 1,000,000?" Doing it with the chart or manually on the calc would take forever. Writing a program on the calc would be faster. But then I thought, wouldn't a computer be really fast? So basically, I'm trying to create as many trials as possible, even though it's complete overkill. I don't mean to ask anyone to do everything for me.

The outcome of rolling a physical dice depends upon environmental variables in your physical surroundings. Random algorithms, that generate pseudo-random numbers, do not take any physical properties into play. The outcomes of each will differ.
Because random number generators in computers won't match the physics that judge how a dice will roll. I was thinking maybe you wanted to figure out what's the "luckiest" number to guess in gambling, for instance, which wouldn't be accurate to use a computer for.
Whoa. I get the sense that this is the tip of the iceburg of a really big and complex issue...
I take it that a calculator (and a computer) will generate psudo-random numbers then. If so then I would think that that's ok.
 
Do you have a programmable calculator like a TI-83, TI-84, or TI-89?
 
If you are decent at basic and have a basic compiler or interpreter, you could write a couple of simple programs that would do 10s or even 100s of thousands of trials on you rig in seconds.

LOL ... Ford and I do tend to over-engineer everything don't we? :toast:
 
I have a TI-83 (plus). And I have 1 clarification. When I said basic, I meant whatever language is on the TI-83. I've heard that it's really similar to Basic. And I have neither a compiler nor interpreter.

10s or 100s of thousands of trials in a few seconds is exactly what I'm looking for. I might run this overnight. :cool:
 
I don't have a TI calculator, nor am I familiar in programming one, so I will hand this one off to FordGt.

Not sure how many calculations the calculator will do per second, but your Q6600 would eat this job for lunch and beg for leftoevers :D
 
You can do it on a TI-83+. The only potential problem is that it might run out of memory or the value may be too big for it to handle. :x

1,000 run:
#1: 6.22
#2: 15.72

1,000,000 run:
#1: 5.99
#2: 15.69

It takes just over 1 second (1.09375 sec to be exact) for the 1,000,000 run on my system (utilizing only one core).

#1: 5.99
#2: 15.7
 
1,000,000,000 run:
#1: 6
#2: 15.7

19 minutes, 11.25 seconds (single core). If it were multithreaded, it could probably be done in under 3 minutes on my system.


Your TI-83+ might give different numbers than the .NET framework. It depends on how random the random number generator is. It's not surpising how consistent the tests are seeing as computers aren't all that random when it comes down to it.


Basically, you'll need one for loop and one while loop. The for loop repeats the while loop how ever many times you want to run it. The while loop checks if the random number is good and keeps count of the attempts.

For the second one, you'll have to make a separate program which checks all the indicies of an array for a 0 value. Instead of checking the value, you use that new program to see if all values were hit at least once. Remember to increment your counter inside of the while loop.

For both, keep a running sum and the count in order to get a total. Your calculator manual will help a lot here. Especially look up the syntax for the following:
rand
while
for
 
Last edited:
You need a language and a compiler for that language. VB.NET is a good, easy place to start but that's really only intended for Windows (98 SE thru 7).
 
Hm. Its almost 2 here so for the sake of simplicity, I'll just use my calculator.

Edit 1: Ok, got the first one done. TI-83 is slooow. Does ~2.3 trials/sec. Takes 7 min for 1000.

Working on 2nd one now. 2:30am and counting.

Edit 2: I gotta stop, it's too late. I'm having problems with the second program. First I fill a list with six zeros. Then, I make a random integer 1-6. I store it in a list to it's corresponding index. So if the random number was 1 then it gets stores in L1(1). An index with a value of 0 means that index has not been chosen yet.

I'm having trouble getting the checking loops to work. No time to explain it now, but basically the checking loops go like this. Check L1(1) for value of 0. If true, stop checking and go back to generating random numbers. If L1(1) is not zero check L1(2). (and so on) If L1(6) is not zero, break from all loops and display how many times the main while loop ran.

Tomorrow is gonna suck. I don't do well with little sleep. :banghead:
 
Last edited:
Back
Top