Solidity - Store random numbers to a dynamic array and return the array
I use remix IDE. When I call the function get number I got this 0: uint256:
How can I change it to return the dynamic array's numbers?
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint numbers;
function randomnumber() public view returns (uint){
uint random = uint(sha3(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint){
return numbers;
}
}
ethereum solidity smartcontracts
add a comment |
I use remix IDE. When I call the function get number I got this 0: uint256:
How can I change it to return the dynamic array's numbers?
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint numbers;
function randomnumber() public view returns (uint){
uint random = uint(sha3(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint){
return numbers;
}
}
ethereum solidity smartcontracts
The return statement is not the problem. Just tested on remix too. With a dynamic array. My guess is that your array is empty when you return it. You can check the size before you return it to see this is the case.
– nikos fotiadis
Nov 19 '18 at 22:15
push() adds a new zero-initialized element to the dynamic array and returns the new length. To set the value of the new element you need to then directly assign it: solidity.readthedocs.io/en/develop/… Also, you're filling the entire array with the same (pseudo-random) number - is that the intention?
– sofend
Nov 19 '18 at 23:28
@sofend The early return means that only one iteration of the loop will happen.
– smarx
Nov 20 '18 at 4:13
@sofend i will replace the pseudo number later with Oracle or something i just stack to the basic structure for now
– Bandock
Nov 20 '18 at 19:03
add a comment |
I use remix IDE. When I call the function get number I got this 0: uint256:
How can I change it to return the dynamic array's numbers?
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint numbers;
function randomnumber() public view returns (uint){
uint random = uint(sha3(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint){
return numbers;
}
}
ethereum solidity smartcontracts
I use remix IDE. When I call the function get number I got this 0: uint256:
How can I change it to return the dynamic array's numbers?
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint numbers;
function randomnumber() public view returns (uint){
uint random = uint(sha3(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint){
return numbers;
}
}
ethereum solidity smartcontracts
ethereum solidity smartcontracts
edited Nov 19 '18 at 23:19
dferenc
4,591122231
4,591122231
asked Nov 19 '18 at 20:16
BandockBandock
31
31
The return statement is not the problem. Just tested on remix too. With a dynamic array. My guess is that your array is empty when you return it. You can check the size before you return it to see this is the case.
– nikos fotiadis
Nov 19 '18 at 22:15
push() adds a new zero-initialized element to the dynamic array and returns the new length. To set the value of the new element you need to then directly assign it: solidity.readthedocs.io/en/develop/… Also, you're filling the entire array with the same (pseudo-random) number - is that the intention?
– sofend
Nov 19 '18 at 23:28
@sofend The early return means that only one iteration of the loop will happen.
– smarx
Nov 20 '18 at 4:13
@sofend i will replace the pseudo number later with Oracle or something i just stack to the basic structure for now
– Bandock
Nov 20 '18 at 19:03
add a comment |
The return statement is not the problem. Just tested on remix too. With a dynamic array. My guess is that your array is empty when you return it. You can check the size before you return it to see this is the case.
– nikos fotiadis
Nov 19 '18 at 22:15
push() adds a new zero-initialized element to the dynamic array and returns the new length. To set the value of the new element you need to then directly assign it: solidity.readthedocs.io/en/develop/… Also, you're filling the entire array with the same (pseudo-random) number - is that the intention?
– sofend
Nov 19 '18 at 23:28
@sofend The early return means that only one iteration of the loop will happen.
– smarx
Nov 20 '18 at 4:13
@sofend i will replace the pseudo number later with Oracle or something i just stack to the basic structure for now
– Bandock
Nov 20 '18 at 19:03
The return statement is not the problem. Just tested on remix too. With a dynamic array. My guess is that your array is empty when you return it. You can check the size before you return it to see this is the case.
– nikos fotiadis
Nov 19 '18 at 22:15
The return statement is not the problem. Just tested on remix too. With a dynamic array. My guess is that your array is empty when you return it. You can check the size before you return it to see this is the case.
– nikos fotiadis
Nov 19 '18 at 22:15
push() adds a new zero-initialized element to the dynamic array and returns the new length. To set the value of the new element you need to then directly assign it: solidity.readthedocs.io/en/develop/… Also, you're filling the entire array with the same (pseudo-random) number - is that the intention?
– sofend
Nov 19 '18 at 23:28
push() adds a new zero-initialized element to the dynamic array and returns the new length. To set the value of the new element you need to then directly assign it: solidity.readthedocs.io/en/develop/… Also, you're filling the entire array with the same (pseudo-random) number - is that the intention?
– sofend
Nov 19 '18 at 23:28
@sofend The early return means that only one iteration of the loop will happen.
– smarx
Nov 20 '18 at 4:13
@sofend The early return means that only one iteration of the loop will happen.
– smarx
Nov 20 '18 at 4:13
@sofend i will replace the pseudo number later with Oracle or something i just stack to the basic structure for now
– Bandock
Nov 20 '18 at 19:03
@sofend i will replace the pseudo number later with Oracle or something i just stack to the basic structure for now
– Bandock
Nov 20 '18 at 19:03
add a comment |
2 Answers
2
active
oldest
votes
Try this
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint numbers;
function randomnumber() public returns (uint){
uint random = uint(keccak256(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint){
return numbers;
}
}
1
randomnumber
doesn't need to be payable.
– nikos fotiadis
Nov 20 '18 at 10:33
Yeh. i updated answer
– Mahesh Rajput
Nov 20 '18 at 10:37
Thank you very much both
– Bandock
Nov 20 '18 at 18:55
add a comment |
The function is a view
, so it can't modify state. Calling randomnumber()
will return a value, but it won't change the numbers
array.
Drop the view
modifier from randomnumber()
, and it will add one item to the array. (The early return will prevent the loop from repeating.)
Thank you i get it now
– Bandock
Nov 20 '18 at 19:00
Why is there a loop at all?
– sofend
Nov 20 '18 at 22:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382000%2fsolidity-store-random-numbers-to-a-dynamic-array-and-return-the-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint numbers;
function randomnumber() public returns (uint){
uint random = uint(keccak256(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint){
return numbers;
}
}
1
randomnumber
doesn't need to be payable.
– nikos fotiadis
Nov 20 '18 at 10:33
Yeh. i updated answer
– Mahesh Rajput
Nov 20 '18 at 10:37
Thank you very much both
– Bandock
Nov 20 '18 at 18:55
add a comment |
Try this
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint numbers;
function randomnumber() public returns (uint){
uint random = uint(keccak256(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint){
return numbers;
}
}
1
randomnumber
doesn't need to be payable.
– nikos fotiadis
Nov 20 '18 at 10:33
Yeh. i updated answer
– Mahesh Rajput
Nov 20 '18 at 10:37
Thank you very much both
– Bandock
Nov 20 '18 at 18:55
add a comment |
Try this
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint numbers;
function randomnumber() public returns (uint){
uint random = uint(keccak256(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint){
return numbers;
}
}
Try this
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint numbers;
function randomnumber() public returns (uint){
uint random = uint(keccak256(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint){
return numbers;
}
}
edited Nov 20 '18 at 10:50
answered Nov 20 '18 at 9:54
Mahesh RajputMahesh Rajput
2959
2959
1
randomnumber
doesn't need to be payable.
– nikos fotiadis
Nov 20 '18 at 10:33
Yeh. i updated answer
– Mahesh Rajput
Nov 20 '18 at 10:37
Thank you very much both
– Bandock
Nov 20 '18 at 18:55
add a comment |
1
randomnumber
doesn't need to be payable.
– nikos fotiadis
Nov 20 '18 at 10:33
Yeh. i updated answer
– Mahesh Rajput
Nov 20 '18 at 10:37
Thank you very much both
– Bandock
Nov 20 '18 at 18:55
1
1
randomnumber
doesn't need to be payable.– nikos fotiadis
Nov 20 '18 at 10:33
randomnumber
doesn't need to be payable.– nikos fotiadis
Nov 20 '18 at 10:33
Yeh. i updated answer
– Mahesh Rajput
Nov 20 '18 at 10:37
Yeh. i updated answer
– Mahesh Rajput
Nov 20 '18 at 10:37
Thank you very much both
– Bandock
Nov 20 '18 at 18:55
Thank you very much both
– Bandock
Nov 20 '18 at 18:55
add a comment |
The function is a view
, so it can't modify state. Calling randomnumber()
will return a value, but it won't change the numbers
array.
Drop the view
modifier from randomnumber()
, and it will add one item to the array. (The early return will prevent the loop from repeating.)
Thank you i get it now
– Bandock
Nov 20 '18 at 19:00
Why is there a loop at all?
– sofend
Nov 20 '18 at 22:13
add a comment |
The function is a view
, so it can't modify state. Calling randomnumber()
will return a value, but it won't change the numbers
array.
Drop the view
modifier from randomnumber()
, and it will add one item to the array. (The early return will prevent the loop from repeating.)
Thank you i get it now
– Bandock
Nov 20 '18 at 19:00
Why is there a loop at all?
– sofend
Nov 20 '18 at 22:13
add a comment |
The function is a view
, so it can't modify state. Calling randomnumber()
will return a value, but it won't change the numbers
array.
Drop the view
modifier from randomnumber()
, and it will add one item to the array. (The early return will prevent the loop from repeating.)
The function is a view
, so it can't modify state. Calling randomnumber()
will return a value, but it won't change the numbers
array.
Drop the view
modifier from randomnumber()
, and it will add one item to the array. (The early return will prevent the loop from repeating.)
answered Nov 20 '18 at 4:15
smarxsmarx
48.6k46173
48.6k46173
Thank you i get it now
– Bandock
Nov 20 '18 at 19:00
Why is there a loop at all?
– sofend
Nov 20 '18 at 22:13
add a comment |
Thank you i get it now
– Bandock
Nov 20 '18 at 19:00
Why is there a loop at all?
– sofend
Nov 20 '18 at 22:13
Thank you i get it now
– Bandock
Nov 20 '18 at 19:00
Thank you i get it now
– Bandock
Nov 20 '18 at 19:00
Why is there a loop at all?
– sofend
Nov 20 '18 at 22:13
Why is there a loop at all?
– sofend
Nov 20 '18 at 22:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382000%2fsolidity-store-random-numbers-to-a-dynamic-array-and-return-the-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The return statement is not the problem. Just tested on remix too. With a dynamic array. My guess is that your array is empty when you return it. You can check the size before you return it to see this is the case.
– nikos fotiadis
Nov 19 '18 at 22:15
push() adds a new zero-initialized element to the dynamic array and returns the new length. To set the value of the new element you need to then directly assign it: solidity.readthedocs.io/en/develop/… Also, you're filling the entire array with the same (pseudo-random) number - is that the intention?
– sofend
Nov 19 '18 at 23:28
@sofend The early return means that only one iteration of the loop will happen.
– smarx
Nov 20 '18 at 4:13
@sofend i will replace the pseudo number later with Oracle or something i just stack to the basic structure for now
– Bandock
Nov 20 '18 at 19:03