Solidity - Store random numbers to a dynamic array and return the array












0














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;
}
}









share|improve this question
























  • 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 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 at 23:28










  • @sofend The early return means that only one iteration of the loop will happen.
    – smarx
    Nov 20 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 at 19:03
















0














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;
}
}









share|improve this question
























  • 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 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 at 23:28










  • @sofend The early return means that only one iteration of the loop will happen.
    – smarx
    Nov 20 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 at 19:03














0












0








0







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;
}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 23:19









dferenc

4,487122030




4,487122030










asked Nov 19 at 20:16









Bandock

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 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 at 23:28










  • @sofend The early return means that only one iteration of the loop will happen.
    – smarx
    Nov 20 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 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 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 at 23:28










  • @sofend The early return means that only one iteration of the loop will happen.
    – smarx
    Nov 20 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 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 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 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 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 at 23:28












@sofend The early return means that only one iteration of the loop will happen.
– smarx
Nov 20 at 4:13




@sofend The early return means that only one iteration of the loop will happen.
– smarx
Nov 20 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 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 at 19:03












2 Answers
2






active

oldest

votes


















1














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;
}
}





share|improve this answer



















  • 1




    randomnumber doesn't need to be payable.
    – nikos fotiadis
    Nov 20 at 10:33










  • Yeh. i updated answer
    – Mahesh Rajput
    Nov 20 at 10:37










  • Thank you very much both
    – Bandock
    Nov 20 at 18:55



















1














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






share|improve this answer





















  • Thank you i get it now
    – Bandock
    Nov 20 at 19:00










  • Why is there a loop at all?
    – sofend
    Nov 20 at 22:13











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
});


}
});














draft saved

draft discarded


















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









1














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;
}
}





share|improve this answer



















  • 1




    randomnumber doesn't need to be payable.
    – nikos fotiadis
    Nov 20 at 10:33










  • Yeh. i updated answer
    – Mahesh Rajput
    Nov 20 at 10:37










  • Thank you very much both
    – Bandock
    Nov 20 at 18:55
















1














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;
}
}





share|improve this answer



















  • 1




    randomnumber doesn't need to be payable.
    – nikos fotiadis
    Nov 20 at 10:33










  • Yeh. i updated answer
    – Mahesh Rajput
    Nov 20 at 10:37










  • Thank you very much both
    – Bandock
    Nov 20 at 18:55














1












1








1






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;
}
}





share|improve this answer














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;
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 10:50

























answered Nov 20 at 9:54









Mahesh Rajput

2767




2767








  • 1




    randomnumber doesn't need to be payable.
    – nikos fotiadis
    Nov 20 at 10:33










  • Yeh. i updated answer
    – Mahesh Rajput
    Nov 20 at 10:37










  • Thank you very much both
    – Bandock
    Nov 20 at 18:55














  • 1




    randomnumber doesn't need to be payable.
    – nikos fotiadis
    Nov 20 at 10:33










  • Yeh. i updated answer
    – Mahesh Rajput
    Nov 20 at 10:37










  • Thank you very much both
    – Bandock
    Nov 20 at 18:55








1




1




randomnumber doesn't need to be payable.
– nikos fotiadis
Nov 20 at 10:33




randomnumber doesn't need to be payable.
– nikos fotiadis
Nov 20 at 10:33












Yeh. i updated answer
– Mahesh Rajput
Nov 20 at 10:37




Yeh. i updated answer
– Mahesh Rajput
Nov 20 at 10:37












Thank you very much both
– Bandock
Nov 20 at 18:55




Thank you very much both
– Bandock
Nov 20 at 18:55













1














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






share|improve this answer





















  • Thank you i get it now
    – Bandock
    Nov 20 at 19:00










  • Why is there a loop at all?
    – sofend
    Nov 20 at 22:13
















1














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






share|improve this answer





















  • Thank you i get it now
    – Bandock
    Nov 20 at 19:00










  • Why is there a loop at all?
    – sofend
    Nov 20 at 22:13














1












1








1






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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 4:15









smarx

48.1k45971




48.1k45971












  • Thank you i get it now
    – Bandock
    Nov 20 at 19:00










  • Why is there a loop at all?
    – sofend
    Nov 20 at 22:13


















  • Thank you i get it now
    – Bandock
    Nov 20 at 19:00










  • Why is there a loop at all?
    – sofend
    Nov 20 at 22:13
















Thank you i get it now
– Bandock
Nov 20 at 19:00




Thank you i get it now
– Bandock
Nov 20 at 19:00












Why is there a loop at all?
– sofend
Nov 20 at 22:13




Why is there a loop at all?
– sofend
Nov 20 at 22:13


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


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




draft saved


draft discarded














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





















































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







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]