Single Random Number?
Author |
Message |
Taz
|
Posted: Tue Nov 18, 2008 4:20 pm Post subject: Single Random Number? |
|
|
Hiya. I need help with a very simple problem. I did a bit of searching on the forums, but I couldn't find exactly what I was having trouble with.
Actually, it's not even so much a problem.. But.
The idea here is that I'm going to have an amount of people, who would say, write their name on a piece of paper.
All of the papers would then be stuck in a hat, and a single paper would be drawn.
Whoever's name was on the paper wins. [So it's basically a lottery!]
I took a look at Turing's help file, and I came up with this..
-
var i : int
var MAX : int
put "Participants : "..
get MAX
randint ( i, 1, MAX )
put "The winner is number ", i,"!"
-
This seems fine to me.. but before I added the 'MAX' and 'get' prompt, I would keep hitting 'Run' and somethines the program would simply give me the same number 3 - 5 times in a row, even when it was a 1 - 100 choice.
Is it really random, and is it being influenced by something?
I want my little lottery program to be as far as possible, because it's actually going to involve real people.
[And to be honest, this doesn't need to be done in Turing. If anyone knows a website or something that would accomplish the same thing for me, that'd be just fine. But I figure I know a tiny bit of turing, so why not try to make it myself? ]
Thanks in advance! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Tue Nov 18, 2008 4:45 pm Post subject: RE:Single Random Number? |
|
|
Don't use randint (). Jusr don't. Use Rand.Int(). It does basically the same thing, but doesn't require a variable. It's used like so:
code: |
put Rand.Int (1, MAX)
|
This takes one less variable, and does the job better than randint(). A note on variables: The first letter of a variable name cannot be a capital. It must be lower case, though subsequent letters may be upper case.
var foo = good
var Foo = bad
var foO = good
var FoO = bad |
|
|
|
|
|
Taz
|
Posted: Tue Nov 18, 2008 4:55 pm Post subject: RE:Single Random Number? |
|
|
Thanks!
But I'm wondering why you say that about the variable names?
It worked fine for me. |
|
|
|
|
|
cavetroll
|
Posted: Tue Nov 18, 2008 5:09 pm Post subject: Re: Single Random Number? |
|
|
Well it's not actually random. There is an algorithm that calculates the number (it's pseudo-random). The number it generates is (usually) based off the time the number is generated at. With a computer, it is impossible to have an real random number. The only way (that I know of) is to use radioactive decay.
That being said, the randomness is good enough to be used for something like this. The pattern repeats after something like 2^19937 (depends on the algorithm) iterations. The only time this wouldn't be secure enough would be for something like cryptography.
As for repeated results, if you are running the program really fast over and over again, it's possible it is using the same seed. If you leave a few seconds between each trial, you shouldn't get repeated results other than is statistically probable. If you are, I would try combining a few random numbers together. This would give you more chance for variation. |
|
|
|
|
|
copthesaint
|
Posted: Tue Nov 18, 2008 5:15 pm Post subject: RE:Single Random Number? |
|
|
I do recall Rand.Seed Which is Always random.
I'm not possitive if or what the proper code is for turing. |
|
|
|
|
|
cavetroll
|
Posted: Tue Nov 18, 2008 5:17 pm Post subject: RE:Single Random Number? |
|
|
Are you sure?
I thought Rand.Seed just let you pick what you used as a seed (as opposed to the default of the time). I figure that this would make it less random. |
|
|
|
|
|
Taz
|
Posted: Tue Nov 18, 2008 5:23 pm Post subject: RE:Single Random Number? |
|
|
Thanks a ton, cavetroll. No idea what radioactive decay is, but I appreciate the in-depth response.
You're probably right about the "running the program really fast over and over again" bit. That was most likely the problem there. =D |
|
|
|
|
|
gitoxa
|
Posted: Tue Nov 18, 2008 5:27 pm Post subject: Re: RE:Single Random Number? |
|
|
If you were to use the random function multiple times in one execution, they'll return different values. So unless you're going ot run your program by compiling it to only create one random number per execution, then it should be fine.
As for the variable names, what insectoid said was how his coding conventions are for variable names. Well, it's a lot of people's on this boards preference. Not everyone uses the same conventions though, for example the ones our teacher likes are:
VariableName
Procedure_Or_Function
CONSTANT
USER_DEFINED_TYPE
Almost any convention is good so long as you stick with it. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Taz
|
Posted: Tue Nov 18, 2008 5:41 pm Post subject: RE:Single Random Number? |
|
|
Oh, I understand.
It's about consistency, right?
I guess I've never wrote anything that had much to it. Usually a variable or 3, as I'm just learning, really. =D |
|
|
|
|
|
Jack140
|
Posted: Tue Nov 18, 2008 6:04 pm Post subject: Re: Single Random Number? |
|
|
I hope this kind of helps...
You just have to make more variables for the amount of people who is involved...
code: |
var i : int
var MAX : int
var name1 : string %Names of people
var name2 : string
var name3 : string
put "How many Participants are there? : ".. %Get the names of your participants
get MAX
put "Name of a participant(#1)"..
get name1
put "Name of Second Participant (#2)"..
get name2
put "Name of Third Participant(#3)"..
get name3
put "and the winner is... (drum roll please...)"
delay (2500) %Hear heart beats pounding across the room, lol
randint ( i, 1, MAX )
put "The winner is ", i,"!" %Outcomes the results |
|
|
|
|
|
|
andrew.
|
Posted: Tue Nov 18, 2008 6:26 pm Post subject: RE:Single Random Number? |
|
|
Turing: | var num_of_people : int
put "How many people are in this draw?"
get num_of_people
var names : array 1..num_of_people of string
cls
for i : 1..num_of_people
put "Contestant #"+i+ ", what is your name?"
get names (i )
cls
end for
var winner : string := names (Rand.Int (1, num_of_people ))
put "The winner is... "+winner+ "! Congrats!" |
Since this isn't for school I suppose I can give you the code. |
|
|
|
|
|
copthesaint
|
Posted: Tue Nov 18, 2008 8:53 pm Post subject: Re: Single Random Number? |
|
|
That's good andrew but you made 1 mistake ohh well
code: | var num_of_people : int
put "How many people are in this draw?"
get num_of_people
var names : array 1..num_of_people of string
cls
for i : 1..num_of_people
put ("Contestant #"),i, (", what is your name?" )% <-mistake was originaly this line
get names (i)
cls
end for
var winner : string := names (Rand.Int (1, num_of_people))
put "The winner is... "+winner+"! Congrats!" |
Now your program will work |
|
|
|
|
|
Taz
|
Posted: Wed Nov 19, 2008 1:31 am Post subject: RE:Single Random Number? |
|
|
Thanks, really. That turned out nicely. It was great having it actually tell me the name I entered instead of a number/
But now I'm going to have 3 winners out of the bunch.
I think I saw a thread around here that would tell me how to find a random number between a 1 and something, and exclude numbers it already picked.. So I'll look around.
[Though I'd appreciate it if anyone can tell me how to edit it, as I'm going to bed now. =D] |
|
|
|
|
|
andrew.
|
Posted: Wed Nov 19, 2008 6:32 pm Post subject: RE:Single Random Number? |
|
|
You could make another array of boolean values with the same number of elements and when a number is chosen, then the corresponding value in the boolean array is marked as true as opposed to false. Everytime you pick a name, you check to see whether it has been picked by checking the boolean array. If it has been picked then try another number. |
|
|
|
|
|
|
|