Random Number Generator?
Author |
Message |
yuvan
|
Posted: Tue May 21, 2013 4:44 pm Post subject: Random Number Generator? |
|
|
What is it you are trying to achieve?
I have to make a program that draws dots using a "randint", but I have to make it in a way that it never puts a dot in the same place twice.
What is the problem you are having?
I cannot figure out how to actually make it so that it won't pick the same number twice.
Describe what you have tried to solve this problem
I tried putting in 2d and 3d arrays but could not get it to work. I am new to programming and don't know what I'm doing so sorry if what I say does not make sense.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
It doesn't work... I need help...
Turing: |
setscreen ("graphics:800;600")
var x, y, n : int := 0
var storex : array 1.. 480000 of int
var storey : array 1.. 480000 of int
% proc store
for i : 1.. 480000
randint (x, 1, 800)
storex (i ) := x
end for
for i : 801.. 480000
storex (i ) := 0
end for
for i : 1.. 480000
randint (y, 1, 600)
storey (i ) := y
end for
for i : 601.. 480000
storex (i ) := 0
end for
% end store
for i : 1.. 800
n := n+ 1
drawdot (storex (i ), storey (i ), red)
end for
delay (1111)
put n
|
Please specify what version of Turing you are using
4.1.1. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
andrew.
|
Posted: Tue May 21, 2013 5:58 pm Post subject: RE:Random Number Generator? |
|
|
Well think about how you would do it yourself. You'd have to remember where your previous dots were, right? So how could you save the locations of the previous dots? |
|
|
|
|
|
yuvan
|
Posted: Tue May 21, 2013 6:17 pm Post subject: RE:Random Number Generator? |
|
|
That's the part I get troubled with in every code i right, saving what the program has already done, could you help out? |
|
|
|
|
|
Tony
|
Posted: Tue May 21, 2013 6:18 pm Post subject: RE:Random Number Generator? |
|
|
Try to think of the case where you somehow already filled up most of the screen, and you are down to the last 2 of 480,000 locations. What kind of setup would make it easier to randomly pick between those two points?
It should be obvious that the chances of picking a random point from the entire screen and ending up in 1 of those 2 spots is very low. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Zren
|
Posted: Tue May 21, 2013 6:21 pm Post subject: Re: Random Number Generator? |
|
|
Quote: I have to make a program that draws dots using a "randint", but I have to make it in a way that it never puts a dot in the same place twice.
You can:
A) Create a list of all coordinates possible and shuffle it. Then only pick the first ___ coordinates in that list. This method would use windowWidth*windowHeight*2 integers of memory.
B) Remember the coordinates you've drawn to before, then check the list you'be made before to see if you can draw there. This method can possibly take much longer when the number of 'random points' reaches closer to the number the maximum number of points available (windowWidth*windowHeight).
C) Iterate over all possible coordinates, incrementing by a random amount each time. This method will have a variable amount of points.
Eg:
Edit: Source code snipped till you've tried it first.
Which can turn into this:
Note: I'm glad I checked if for loops can have dynamic iterators in Turing first... (Answer: They don't)
EDIT: AAAAAAAH. Damn it. Kept getting distracted (someone gave me something shiny while I was writing this).
Edit2: Crap. Now I've just given the/a answer. Grrrr. |
|
|
|
|
|
|
|